javascript - Set var equal selected value from dynamically created select lisst -



javascript - Set var equal selected value from dynamically created select lisst -

html: have select list class, no options.

<select class="myclass"></select>

js: populate select list $.each() iterate through json array

$.each(value,function(key2, value2){ if(key2 == 'name'){ $('.myclass'). append($("<option></option>"). attr("data-catid",value['catid']). attr("data-catiid",value['catiid']). attr("value",key). text(value2)); } });

how select selected option?

$('.myclass').on(change,funciton(){ alert($(this+' option:selected').data('catid')); alert($('.myclass option:selected').data('catid')); alert($('.myclass').is(':selected').data('catid')); ??? none of these work });

you can utilize next data:

$('.myclass').on('change', function(){ alert($("option:selected", this).data('catid')); } );

additionally, may want utilize console.log(...) instead of alert(...) if using firebug (it avoids intrusive , blocking popups).

javascript jquery select option

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -