I have search before I post, but I only found questions and solutions that is showing ONE textbox if ONE checkbox is checked as in: jquery - show textbox when checkbox checked
But my form is different
<form method="post" action="actionhere">
<div id='clone_me'>
<?php
for($i=1; $i<=5;$i++) {
?>
<div>
<span id="title">Line <?php echo $i;?></span>
<input type='checkbox' name='ck_<?php echo $i;?>'/>
<input type='text' class='tx<?php echo $i;?>' name='tx[<?php echo $i;?>][]'/>
<input type='text' class='tx<?php echo $i;?>' name='tx[<?php echo $i;?>][]'/>
<input type='text' class='tx<?php echo $i;?>' name='tx[<?php echo $i;?>][]'/>
<input type='text' class='tx<?php echo $i;?>' name='tx[<?php echo $i;?>][]'/>
</div>
<?php } ?>
</div>
<input type='submit' name='submit' value='Submit'/>
<input type="button" value="Add row" class="addrow" />
</form>
<script>
$(document).ready(function() {
$('.addrow').click(function(){
var n= $("#clone_me>div").length;
var new_n=n+1;
var new_line= $('#clone_me div:first').clone().append();
$("span#title", new_line).text("Line "+new_n+" ");
//since I clone the 1st <div> therefore the cloned id must be 'ck_1', so changed it to latest id
$("input#ck_1", new_line).attr("name", "ck_"+new_n);
$("input#ck_1", new_line).attr("id", "ck_"+new_n);
$("input.tx1", new_line).attr("name", "tx["+new_n+"][]");
$("input.tx1", new_line).attr("class", "tx"+new_n);
new_line.appendTo('#clone_me');
});
});
</script>
As you can see from the code, I have a form which by default will have 5 sets of 1-checkbox-4-textbox, and user is allowed to add new set by clicking 'add row' button (which jquery will do clone).
How can I make the 4 textbox enabled once the correspond checkbox is checked? I'm not using hide() or show(). I want the user knows the textbox are there, but is disabled until user tick the checkbox.
if ($("#ck_4").is(":checked")) {
$("input#tx4).attr("readonly", false); //or enabled
I thought it will be something like that, but since user can dynamically add how many rows as he wants, how can I achieve it?