-1

I have a Table as shown below

<table id="sample_1" aria-describedby="sample_1_info">
   <thead>
      <tr>
         <th><input type="checkbox" id="selecctall"></th>
         <th>Status</th>
      </tr>
   </thead>
   <tbody class="odd gradeX">
      <tr>
         <td width="5%"><input type="checkbox" class="checkbox1" id="1"></td>
         <td width="37%">Chips And Chocolates,Bummy Chips,Plain Salted,ytrytr,hyryr</td>
      </tr>
      <tr>
         <td width="5%"><input type="checkbox" class="checkbox1" id="2"></td>
         <td width="37%">Chips And Chocolates</td>
      </tr>
   </tbody>
</table>


       <input type="button" class="btn blue" value="Delete" id="deletebtn">

       $('#selecctall').click(function(event) {  //on click 
if(this.checked) { // check select status
    $('.checkbox1').each(function() { //loop through each checkbox
        this.checked = true;  //select all checkboxes with class "checkbox1"               
    });
}else{
    $('.checkbox1').each(function() { //loop through each checkbox
        this.checked = false; //deselect all checkboxes with class "checkbox1"                       
    });         
}

});

$( "#deletebtn" ).click(function() {
  var $checked = $('#sample_1').find(":checkbox:checked");

      if (!$checked.length) {
        alert('Need to select atlease one checkbox button');
        event.stopImmediatePropagation();
    }

     else {

       // Here 

     }

});

http://jsfiddle.net/tvbucy4y/

Pawan
  • 31,545
  • 102
  • 256
  • 434

4 Answers4

2

You can try this one. DEMO See the console.

    $('#deletebtn').click(function() {
        var array = $('#sample_1 input:checked');
        var ids = new Array();
        $.each(array, function(idx, obj) {
            ids.push($(obj).attr('id'));

        });
        console.log(ids);
    });
vaso123
  • 12,347
  • 4
  • 34
  • 64
2

Try this code

var chkIds = $('#sample_1 :checkbox:checked:not(#selecctall)').map(function(i,n) {
    return $(n).attr('id');
}).get().join(',');
    alert(chkIds);

Answer Updated: you have to use:not() to exclude the first CheckBox.

prog1011
  • 3,425
  • 3
  • 30
  • 57
1

Use this:

else {
     var ids = [];
     $.each($checked, function(i,e){
        console.log(e);
        if ($(e).attr("id") != 'selecctall'){
            ids.push($(e).attr("id"))
        }
 });
artm
  • 8,554
  • 3
  • 26
  • 43
0

I'd suggest not bothering to get the id of the checked elements, and simply use the nodes:

$("#deletebtn").click(function () {
    // retrieve a jQuery collection of the checked <input>s from the <tbody>
    // which means we ignore the 'select-all' checkbox):
    var $checked = $('#sample_1 tbody input[type="checkbox"]:checked');

    // if we have any checked elements:
    if ($checked.length) {
        // we find the closest <tr> elements to those checked <inputs>, and
        // remove them:
        $checked.closest('tr').remove();
    } else {
        // otherwise, we alert:
        alert("You need to check at least one checkbox.");
    }

});

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410