-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/

3
  • what's the question? Commented Nov 13, 2014 at 11:15
  • If the checkbox is selected how can i fetch its id ?? Commented Nov 13, 2014 at 11:15
  • possible duplicate of Get a list of checked checkboxes in a div using jQuery Commented Nov 13, 2014 at 11:34

4 Answers 4

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);
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Its working fine , can i get the result in form of an array ??
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.

2 Comments

How can i remove selectall from the result ? –
@PreethiJain - See the updated answer. It will exclude the selecctall Checkbox
1

Use this:

else {
     var ids = [];
     $.each($checked, function(i,e){
        console.log(e);
        if ($(e).attr("id") != 'selecctall'){
            ids.push($(e).attr("id"))
        }
 });

1 Comment

How can i remove selectall from the result ?
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:

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.