Jquery Multiple Check Box Enable Text Field
Solution 1:
Updated Answer
Given what you seem to be trying to accomplish I think using classes and data attributes might serve as the easiest and most flexible solution.
OP's HTML (Modified)
<table><tr><td>blah</td><td><inputtype='checkbox'name='a1'class="group_ctrl"data-group="group_a"id='checker'></td><td></td></tr><tr><td><divalign="right">a1:</div></td><td><inputid="tb1"type='text'class="group_a"name='a1_1'size='25'maxlength='5'value='a1_1'></td><td> </td></tr><tr><td><divalign="right">a2:</div></td><td><inputid="tb2"type='text'class="group_a"name='a2_2'size='25'maxlength='5'value='a2_2'></td><td> </td></tr><tr><td><divalign="right">a3:</div></td><td><inputid="tb3"type='text'class="group_a"name='a3_3'size='25'maxlength='5'value='a3_3'></td><td> </td></tr><tr><td><divalign="center">Activate Something Else</div></td><td><inputtype='checkbox'name='b1'class="group_ctrl"data-group="group_b"id='checker2'checked=checked></td><td></td></tr><tr><td><divalign="right">b1:</div></td><td><inputid="tb4"name="b1_1"class="group_b"type="text"size="25"maxlength='5'value='b1_1'></td><td> </td></tr><tr><td><divalign="right">b2:</div></td><td><inputid="tb5"name="b2_2"class="group_b"type="text"size="25"maxlength='5'value='b2_2'></td><td> </td></tr><tr><td><divalign="right">b3:</div></td><td><inputid="tb6"name="b3_3"class="group_b"type="text"size="25"maxlength='5'value='b3_3'></td><td> </td></tr></table>
JQuery
$(document).ready(function() {
$('.group_ctrl').change(function () {
// gets data-group value and uses it in the outer selector// to select the inputs it controls and sets their disabled // property to the negated value of it's checked property
$("." + $(this).data("group")).prop('disabled', !this.checked);
}).change();
});
I have given every input you want to disable/enable a class. E.g. group_[a|b] Then I gave the check-box controls a class group_ctrl and a data attribute named group (data-group) with the corresponding group class it is responsible for controlling as the value. You can duplicate this pattern as many times as needed. E.g. group_c, group_d, group_e... etc.
Solution 2:
Shortest :)
$(document).ready(function () {
$('input[type=checkbox]').change(function () {
$(this).nextUntil('input[type=checkbox]').prop('disabled', !this.checked);
}).change();
});
Solution 3:
Use nextUntil()
to find all text boxes until next check box and toggle them. You are using the IDs of elements, please note that you may also use the type of the element to select and work on them all.
$(document).ready(function () {
// add click function and then call toggleInputs on every checkbox on the page
$('input[type=checkbox]').click(function () {
toggleInputs($(this));
}).each(function () {
toggleInputs($(this));
});
});
functiontoggleInputs(element) {
// careful, it will find all elements until next checkbox
element.nextUntil('input[type=checkbox]').prop('disabled', !element.prop('checked'));
}
But seriously, you need to structure the HTML code properly. if you do, you can use parent()
, siblings()
etc functions much more effectively and meaningfully.
Solution 4:
I think you want to do like this:
<form>
<inputtype="checkbox" name="detailsgiven" id="checker" >
<br>
<inputtype='text' name="details" id="tb1" value='box1'>
<br>
<inputtype='text' name="details" id="tb2" value='box2'>
<br>
<inputtype='text' name="details" id="tb3" value='box3'>
<br><br>
<inputtype="checkbox" name="detailsgiven" id="checker2" >
<br>
<inputtype='text' name="details" id="tb4" value='box1'>
<br>
<inputtype='text' name="details" id="tb5" value='box2'>
<br>
<inputtype='text' name="details" id="tb6" value='box3'>
<br>
</form>
<script>
toggleInputs($('#checker'));
$('#checker').click(function() {
toggleInputs($(this));
});
functiontoggleInputs(element) {
if (element.prop('checked')) {
/*
$('#tb1').prop('disabled', false);
$('#tb2').prop('disabled', false);
$('#tb3').prop('disabled', false);
*/
$(element).parent('form').find('input[type=text]').attr('disabled',false);
} else {
/*
$('#tb1').prop('disabled', true);
$('#tb2').prop('disabled', true);
$('#tb3').prop('disabled', true);
*/
$(element).parent('form').find('input[type=text]').attr('disabled',true);
}
}
</script>
Post a Comment for "Jquery Multiple Check Box Enable Text Field"