My dropdown is very similar to this:
<select id='someSelect'> <option value="0">---select one---</option> <optgroup label="Bikes"> <option value="B-4">Hayabusa</option> <option value="B-2">GSXR</option> <option value="B-3">Ninja</option> <option value="B-6">Enticer</option> </optgroup> <optgroup label="Cars"> <option value="C-4">Audi TT</option> <option value="C-2">Awesome Car</option> <option value="C-23">Japanese car</option> <option value="C-9">German car</option> </optgroup> </select>
I just want to select the 1st element of the 1st group (bikes here). How do I do this in jQuery?
I have currently tried this:
$('#someSelect option:nth-child(1)').attr("selected", "selected");
BUT , the problem is that there are the first three elements ( --select-- , Hayabusa and Audi TT ) that selects all three that finally select the Audi TT
I tried to do some things with each and select only the second, but then I realized that the dropdown is dynamic, I do not want to select the default value ( --select one-- ), but the first element of the first group
I tried to imitate jsfiddle, but it worked and did not work, not sure why: - /
you can see it here
source share