Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  105] [ 6]  / answers: 1 / hits: 30329  / 11 Years ago, tue, april 30, 2013, 12:00:00

I have input text like



<input type=hidden name=xa id=xa data-placeholder=.. style =width: 360px;/>


and i using select2



$(#xa).select2({...});


now, i want to get text when i press 'get text'



$(#gettext).click(function () {
alert($('#xa').val()); // or $('#xa').select2(val);
});


But it get the id of text. It's not text. How can i do it. thanks


More From » jquery

 Answers
19

Select2 creates a data- attribute for internal purpose, were it stores some necessary information like id,selected value of select2 instance. this can used to extract selected value.




Select2 Community : Each element instantiated as a select2 component must contain id and text keys.




var id = $(test).select2('data').id;



var seletedVal = $(test).select2('data').text;



Update : With Latest version of Select2 , the object is stored in a array,so the text has to be accessed as below (jsfiddle link updated as well).



$(test).select2('data')[0].text //instead of $(test).select2('data').text



thanks for update @DiMono



Live Demo @ JSFiddle



Happy Coding :)


[#78508] Monday, April 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margaritakristinak

Total Points: 502
Total Questions: 127
Total Answers: 98

Location: England
Member since Mon, May 17, 2021
3 Years ago
;