mcgrailm
mcgrailm

Reputation: 17640

problems with select option text

I'm trying to get the text value of the select option here and i keep getting the value what am i doing wrong here is the code

 jQuery.each(jQuery('select.personalize_toggle'),function(i){
    var t = jQuery(this).html();
    alert(t);
 });

here is the html

 <select class=" personalize_toggle " >
    <option value="0" ></option>
    <option value="1" >Vargas</option>
 </select>

I want to get 'Vergas' out the select in my jquery each loop

Another try

  jQuery.each(jQuery('select.personalize_toggle option:selected'),function(i){
    var val = jQuery(this).text();
    alert(val); // also alerts 1
 });

Upvotes: 2

Views: 435

Answers (3)

Majid Fouladpour
Majid Fouladpour

Reputation: 30242

In the jQuery code you have select.personalize_toggle but you have spelled it differently in html: class=" personalize_toogle ". So correct the spelling, and use .text() to get the text of the option (as pointed by Sarfraz).

Upvotes: 0

Rwky
Rwky

Reputation: 2144

jQuery('select.personalize_toggle') 

returns each select element from your question you seem to want the html inside the option tags i.e. "Vargas" in this case you should change your selector to

jQuery('select.personalize_toggle option')

This will return each option value and alert the contents.

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382616

You can get it like this:

alert($('#selectbox_id option:selected').text());

Where selectbox_id is the id of the <select> element. If you want to get the value of option, use val() instead of text().

Upvotes: 6

Related Questions