0

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);
});
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
kowshiga
  • 29
  • 6

1 Answers1

1

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>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25