Prashant Bhatt
Prashant Bhatt

Reputation: 79

remove checkbox attribute in jquery clone

my script code and html is like this when i am trying to add new row it is automatically select property of last checkbox if chekbox is checked than in new row add checkbox with check want to remove checkbox checked in new row

script code

<script type="text/javascript">
  $("input.tr_clone_add").live('click', function() {
    var $tr    = $(this).closest('.tr_clone');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $tr.after($clone);
});
</script>  

table look like this

   <table id="table-data" width="100%">
                  <tr class="tr_clone">
                    <td><input type="checkbox" autofocus name="status[]" value="Y"></td>
                    <td>
                      <select name="group_id[]">
                        <option>Select User</option>
                         <?php
                           $selectGroup = "SELECT  group_id,group_name
                                              FROM `group`";
                           $res = mysql_query($selectGroup);
                           while($row = mysql_fetch_array($res))
                           {
                             echo '<option value="'.$row['group_id'].'">'.$row['group_name'].'</option>';
                           }
                          ?>
                      </select>
                    </td>
                    <td><textarea name="address[]" rows="3" cols="35" placeholder="Enter Address"></textarea></td>
                    <td><input type="button" name="add" value="Add" class="tr_clone_add"></td>
                  </tr>
                </table>

Upvotes: 4

Views: 1451

Answers (3)

Samir Lakhani
Samir Lakhani

Reputation: 743

Here is code...

 $("input.tr_clone_add").live('click', function() {

          var $tr    = $(this).closest('.tr_clone');
          var $clone = $tr.clone();
          var $clone = $tr.clone().prop('checked', false);
         //  or var $clone = $tr.clone().removeAttr('checked');

     });

This isn't new with jQuery 3: it was always the case that setting the checked property to false was better than removing the attribute. "In all other cases, .prop( "checked", false ) should be used instead." - I know you're quoting the jQuery people, but that's not correct either: you shouldn't say $(this).prop("checked", false) in cases when you can just say this.checked = false;

Upvotes: 0

Nishit Maheta
Nishit Maheta

Reputation: 6031

check below code . you can find checkbox from cloned element using $clone.find('input[type=checkbox]') and set .attr('checked', false);

working DEMO

 $("input.tr_clone_add").live('click', function() {
  var $tr    = $(this).closest('.tr_clone');
  var $clone = $tr.clone();
  $clone.find(':text').val('');
  $clone.find(':checked').attr('checked', false);
   // or 
  // $clone.find('input[type=checkbox]').attr('checked', false);
  $tr.after($clone);
});

Upvotes: 2

Sougata Bose
Sougata Bose

Reputation: 31749

Remove the checked property. Try with -

var $clone = $tr.clone().prop('checked', false);

OR

var $clone = $tr.clone().removeAttr('checked');

FIDDLE

Upvotes: 1

Related Questions