kowshiga
kowshiga

Reputation: 29

how to get the ids of checked checkboxes in jquery

I want to get the ids of checked checkboxes and store those ids in an array using jquery. Can anybody give me correct code for it.

I have tried

Thanks in advance :)

$("#merge_button").click(function(event){
    event.preventDefault();
    var searchIDs = $("#find-table input:checkbox:checked").map(function(){
        return $(this).val();
    }).toArray();
    console.log(searchIDs);
});

Upvotes: 0

Views: 88

Answers (1)

s.kuznetsov
s.kuznetsov

Reputation: 15213

Use the attr() method to get id. Like this:

$(this).attr('id');

Do you need such a result?

$("#merge_button").click(function(event){
    event.preventDefault();
    var searchIDs = $("#find-table input:checkbox:checked").map(function(){
        return $(this).attr('id');
    }).toArray();
    console.log(searchIDs);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="find-table">
  <input id="1" type="checkbox">
  <input id="2" type="checkbox">
  <input id="3" type="checkbox">
</div>

<button id="merge_button">merge</button>

Upvotes: 1

Related Questions