I want to compare a variable with the select -> - select option to change the "selected" attribute, here is my code, it works, but I think this is not the best way to write it, excuse me English, I used google translate to help hehehehe:
var lista = 'example 1'; $("#id option").each(function(){ if($(this).text() == lista){ $(this).attr('selected','selected'); } });
here is the html:
<select id="id" > <option value="0" >example 1</option> <option value="1" >example 2</option> </select>
here are a few attempts
$('#id option:eq("'+lista+'")').attr('selected','selected') $("#id option[text='"+lista+"']").attr('selected','selected')
kakomerengue
source share