0

how to make the checkbox on the table can be checked? I've the code below to add the checkbox dynamically by using a jquery function. The code below succeed to add the checkbox dynamically, but the problem is the checkbox added by the function can't be checked (disabled).

<table id="detail">
  <tr>
    <td><input type="checkbox" id="cb" name="cb[]"></td>
  </tr>
</table>

This is the button to add the row:

<input type="button" id="addRow" value="ADD ROW" />

And this is the jquery function I have:

<script type="text/javascript">
  $(document).ready(function() {
    $("#addRow").click(function() {
      $('#detail tbody>tr:last').clone(true).insertAfter('#detail tbody>tr:last');
    });
  });
</script>

Anyone can help for the code? Thanks... :)

1
  • Do you want the checkbox added be disabled ?? Commented Sep 21, 2012 at 5:40

3 Answers 3

3

Please first correct the javascript function !

  $(document).ready(function() {
    $("#addRow").click(function() {
      $('#detail tbody>tr:last').clone(true).insertAfter('#detail tbody>tr:last');
    });
  });

And your code works for me means the checkbox is checked (only if you tick last checkbox then your code will clone it)

See my jsfiddle or jsfiddle updated

Sign up to request clarification or add additional context in comments.

3 Comments

Hi thanks for the code. It works! But who should I set the answer? Your answer is the same with @Sushanthreddy but Sushanthreddy is earlier to post the answer.
I corrected your JS code but to solve your problem I posted only jsfiddle (and an update).
Doesn't matter who post first ! the important is the quality of answer.
2

Try this

 $(document).ready(function() {
    $("#addRow").click(function() {
      $('#detail tbody tr:last').clone(true).insertAfter('#detail tbody tr:last');
    });
  }​);

check FIDDLE​

Comments

0
 $("#addRow").click(function() {
     var row = $('#detail tbody>tr:last').clone(true);

     // Clear last value
     row.find("input:checkbox").attr('checked', false);

     // Change name attr
     row.find("#cb").attr('name', 'newNmae');

     //disabled set as a true
     row.find("input:checkbox").attr('disabled', true);

     row.insertAfter('#detail tbody>tr:last');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.