I currently am running this code for a dropdown selection box. When different options are chosen, a class is added to a div element. So far all of this code works without an issue. The problem is I would like to have it work for multiple dropdown selectors on a single page. The div mcga_ has multiples of them which change "mcga_1, mcga_2, etc). I have tried running the code twice and changing the element names, but only one selector will work and the other will not do anything. Here is what works so far;
$(document).ready(function(){
$("select[name*='custom_fields[mcgroup1]']").change(function(){
select_changed();
});
});
function select_changed(){
$("div[id*='mcga_']").each(function(){
$(this).removeClass('addThis');
});
$("select[name*='custom_fields[mcgroup1]']").each(function(){
var selected = $(this).val();
$('#'+selected).addClass('addThis');
});
}
That works without a problem. It adds the class "addThis" to every "mcga_*" div. I tried the following to make it work with a different dropdown as well on the same page, but to no avail.
$(document).ready(function(){
$("select[name*='custom_fields[mcgroup1]']").change(function(){
select_changed();
});
});
function select_changed(){
$("div[id*='mcga_']").each(function(){
$(this).removeClass('addThis');
});
$("select[name*='custom_fields[mcgroup2]']").each(function(){
var selected = $(this).val();
$('#'+selected).addClass('addThis');
});
}
$(document).ready(function(){
$("select[name*='custom_fields[mcgroup2]']").change(function(){
select_changed();
});
});
function select_changed(){
$("div[id*='mcgb_']").each(function(){
$(this).removeClass('addThis2');
});
$("select[name*='custom_fields[mcgroup2]']").each(function(){
var selected = $(this).val();
$('#'+selected).addClass('addThis2');
});
}
Any help to get this code to clean up and work with multiple dropdown menu's will be great.
Cheers!