javascript - select2 atribute "data-init-text" -


i populate select, not have associated description. have value.

i using (select2):

$( '#element' ).attr( "data-init-text", "" ).val( value ).change(); 

but not work because have no description

i'm using hidden input ajax call.

any idea. thank you

you need text (which call description) id (which call value). since using ajax select2 data, use ajax call text id.

you supply initselection function makes ajax call. might this:

initselection: function(element, callback) {     var id= $(element).val();     if (id) {         $.ajax('/this/is/the/url/', {             data: { id: id },             datatype: 'json',             success: function(data) { callback(data); } });     } } 

in case ajax call passes "id" parameter , expected return json looks this:

{ id: 'the_passed_in_id', text: 'the_text_for_the_id' } 

demo on jsfiddle


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

How can I fetch data from a web server in an android application? -

jquery - How can I dynamically add a browser tab? -