I have 3 dropdown where I want to add a class to a specific div depending on the value of the dropdown.
<select name="item1">
<option>Blue</option>
<option>Green</option>
<option>Red</option>
</select>
<select name="item2">
<option>Blue</option>
<option>Green</option>
<option>Red</option>
</select>
<select name="item3">
<option>Blue</option>
<option>Green</option>
<option>Red</option>
</select>
<div class="product">
<div class="item1"></div>
<div class="item2"></div>
<div class="item3"></div>
</div>
I'm trying to add a css class depending on the value of a dropdown. For example, if the option blue from the dropdown item1 is selected, I would like to add a class .blue to the div item1.
I used to have checkboxes instead of the dropdown and I had this code:
$(document).ready(function($){
$('input#red').change(function(){
if($(this).is(":checked")) {
$('div.item1').addClass("red");
} else {
$('div.item1').removeClass("red");
}
});
$('input#blue').change(function(){
if($(this).is(":checked")) {
$('div.item1').addClass("blue");
} else {
$('div.item1').removeClass("blue");
}
});
});
I created this: http://jsfiddle.net/uJcB7/14/ How does should be done correctly?
------------EDIT-----------
I am using gravity forms to generate the select box, and they are adding "|0" after the name of the option (I don't know why!).
For example "green|0" instead of "green". The "|0" is hidden in the field but the class generated is "green|0".
Any thought on how to remove that "|0" on the css class?
Also they generate a name for the selectbox, is it possible to use a class instead of using the name element?
Have a look on the updated code: