Iam using dropdown box which is implemented using JQuery-select2 version 3.5.
I want to set the 2'nd item selected from the list like $("#fieldId").prop("selectedIndex",1)
using JQuery.
Is it possible?
Thanks in advance
Iam using dropdown box which is implemented using JQuery-select2 version 3.5.
I want to set the 2'nd item selected from the list like $("#fieldId").prop("selectedIndex",1)
using JQuery.
Is it possible?
Thanks in advance
$('#fieldId').select2("val",$('#fieldId option:eq(1)').val());
/*By using eq(), the index number should be calculated from 0*/
or
$('#fieldId').select2("val",$('#fieldId option:nth-child(2)').val());
/*By using nth-child(), the index number should be calculated from 1*/
This may be useful
If you mean the select
drop down menu then This should solve your problem
<select>
<option>1st item</option>
<option selected>2nd item</option>
<option>3rd item</option>
</select>
Edit: You can achieve that like this
$('select option:nth-child(2)').attr('selected', true);
$('#selectId').find('option').each(function(index,element){
if(element.value.toLowerCase().trim() == 'VAL_TO_MATCH'){
$("#selectId").prop('selectedIndex', index).change();
}
});