I'm searching for a jQuery plugin that does this.
for example:
<label><input type="checkbox" depends_on="foo=5" name="boo" ... /> Check </label>
<select name="foo" ... >
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
so the checkbox would only be enabled if the "5" option is selected below.
or
<select name="foo" depends_on="boo" ... >
<option value="5" selected="selected">5</option>
<option value="15">15</option>
<option value="25">25</option>
</select>
<label><input type="checkbox" name="boo" ... /> Check </label>
In this case the select should only enabled if "boo" is checked.
I managed to come up with a code that does something like this:
$("*[depends_on]").each(function(){
var source = $(this);
var dep = $(this).attr('depends_on');
var target = dep.split("=");
$("*[name='"+target[0]+"']:not(:hidden)").change(function(){
if($(this).attr('checked')) {
source.removeClass('disabled');
source.removeAttr('disabled');
}
else{
source.attr('disabled', 'disabled');
source.addClass('disabled');
}
}).change();
});
seems to work ok for selects that depend on radios, but I want to implement this for any type of input or combination (or at least most of them), without having to write separate code for each type of input.
anyway, is anyone aware of such a plugin? or maybe have suggestions to my code above? :)