I've seen a lot of help for dynamically adding rows or fields, but I'm interested in controlling fields that are dependent on one another.
For example, I have a form with 3 user inputs:
<select id="foo">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
When #foo
's value is set to 1
, I'd like to enable #bar
.
<!-- by default, bar should be disabled -->
<select id="bar">
<option value="something_1">Something 1</option>
<option value="something_2">Something 2</option>
<option value="other">Other...</option>
</select>
When #bar
's value is set to other
, I'd like to enable #baz
.
<!-- by default, baz should be disabled -->
<textarea id="baz"></textarea>
My Goal
I'd like to some guidance on writing a small plugin that allows easy creation of form inputs with dependencies.
I'd like short, concise syntax. Is there a way I could add html attribute helpers to aid the jQuery plugin in "automating" things?
Foreseeable Issues
- If
#foo
is set to "Yes", then#bar
is set to "other", then#foo
is set to "No", I will want#bar
's dependencies deactivated/hidden as well. - Inputs are enabled/disabled and/or visible/hidden; Having the ability to define custom behaviors for elements would be nice.
Reinventing the Wheel
If there's a plugin out there that does this kind of thing, let me know! I couldn't seem to find one...