The following script enables/disables their nested form controls using a for:x
class name. The problem is that nested for:y
elements are being enabled when they shouldn't be. They should only be enabled when the nested for:y
is enabled.
Can someone provide me with an additional filter to exclude the form elements that are inside a nested for:*
class?
In other words, given the example below, when losssold
is checked then all the child inputs of for:losssold
should be enabled except for the child element elements inside for:has-buy-sell-agreementtrue
if has-buy-sell-agreementtrue
is not also checked.
So I thought it would be easier to just figure to exclude those elements in for:has-buy-sell-agreementtrue
.
<script type="text/javascript">
$('[class*="for:"]').each(function(){
var klass=$(this).attr('class'),element;
var that = this;
$.each(klass.split(' '),function(index,value){
if(!value)return;
if (value.substr(0,4)=='for:')
element=value.substr(4);
});
$('[name="' + $('#'+element).attr('name') + '"]')
.click(function(){
$(that).toggleClass('disabled', !$('#'+element).is(':checked'))
.find('input,select,textarea') /* insert edge case here */
.each(function(){
if ($('#'+element).is(':checked'))
$(this).removeAttr('disabled');
else
$(this).attr('disabled','disabled');
});
})
.trigger('click');
});
</script>
<li>What would you like to see happen to your business when you (or a co-owner) die, become disabled, or retire? <br/>
<ul><li><label><input type="radio" name="loss" id="losssold" value="sold"/> Sold to other Owners/another Business</label>
<ul class="for:losssold">
<li>Do you have a buy-sell agreement?<br/>
<label><input type="radio" name="has-buy-sell-agreement" id="has-buy-sell-agreementtrue" value="true"/> Yes</label>
<label><input type="radio" name="has-buy-sell-agreement" id="has-buy-sell-agreementfalse" value="false"/> No</label><br/>
<ol class="for:has-buy-sell-agreementtrue"> <!-- nested for class; inner elements shouldn't be affected -->
<li><label for="buy-sell-last-updated">When was the agreement last updated?</label><br/>
<input type="text" name="buy-sell-last-updated" id="buy-sell-last-updated" value=""/>
</li>
<li>Is the agreement funded?<br/>
<label><input type="radio" name="buy-sell-is-funded" id="buy-sell-is-fundedtrue" value="true"/> Yes</label>
<label><input type="radio" name="buy-sell-is-funded" id="buy-sell-is-fundedfalse" value="false"/> No</label>
</li>
</ol>
</li>
</ul>
</li>
</ul>
</li>