I have dynamically generated select fields inside a div called mydiv. The id and class names also for each of my select fields are also dynamically generated.
I am trying to add a class called red-border to each of my select elements inside mydiv based on the value.
for example if Not mapped is selected by default its value is " " empty in my first select than class red-border gets added to my first select
Here is my js please help
$('[class^="mydiv"]').find('option').each(function() {
var myvalue= $(this).val();
if (myvalue===" "){
$(this).addClass("red-border");
}else{
$(this).addClass("green-border");
}
});
<div class="mydiv" id="mydiv">
<select id="dynamically_generated_select_169" class="someclass169">
<option value=" ">Not mapped</option>
<option value="1">Saab</option>
<option value="2">VW</option>
<option value="3" selected>Audi</option>
</select>
<!-- Another select Each select and its options are all dynamically generated -->
<select id="dynamically_generated_select_170"class="someclass170">
<option value="1">Saab</option>
<option value=" ">Not mapped</option>
<option value="2">VW</option>
<option value="3" selected>Audi</option>
</select>
</div>
Var myvalue= $(this).val();tovar myvalue= $(this).val();You have spelled var with a V - Carsten Løvbo Andersen.find('option')to.find('select')- Toastrackenigma[class^="mydiv"]. @Toastrackenigma - Berkay.find('option'). The^=operator selects all elements where the attribute begins with the value, i.e. all elements where the class begins withmydivin this case. So this just serves to selectmydiv. Insidemydiv, he is then trying to check and compare eachoptiontag, when instead he should be trying to check and compare eachselecttag. - Toastrackenigma