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

qt - QML MouseArea onWheel event not working properly when inside QML Scrollview -

java - is not an enclosing class / new Intent Cannot Resolve Constructor -

python - Error importing VideoFileClip from moviepy : AttributeError: 'PermissionError' object has no attribute 'message' -