javascript - setting selected value inside combobox using its name not index -


if i'm rendering combobox this

    $('#mycombo').html('<option value=""></option>');     $.each(mydata, function (i, v) {        $('#mycombo').append('<option value="' + v.personid+ '">' + v.personname + '</option>');     }); 

how can set selected value inside combobox if v.personname == 'john'

p.s. know can set value using it's index

 $('#mycombo').prop('selectedindex', 2); 

but in case don't know index, know name.

one simple answer

$('#mycombo').html('<option value=""></option>');     $.each(mydata, function (i, v) {        if (v.personname== 'john')           $('#mycombo').append('<option selected value="' + v.personid+ '">' + v.personname + '</option>');        else           $('#mycombo').append('<option value="' + v.personid+ '">' + v.personname + '</option>');     }); 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -