marchemike
marchemike

Reputation: 3277

Getting id of checkboxes selected using jquery

I'm trying to get the id's of the checkboxes with the class file-selection-id. The checkbox is inside a for loop which is why I have placed variables on the id's and names.

<input class="file-selection-id" type="checkbox" value="" name="file<?php echo $i; ?>_<?php echo $t; ?>" id="file<?php echo $i; ?>_<?php echo $t; ?>">

What I want is that when I click a button, it would get all of the id's of the selected checkboxes.

$('.move-file-buttons').click( 
function(event) { 
  //??? 
$('input:checkbox.file-selection-id').each(function () {

  });
     });

But I don't know how to get the Id's of all the checkboxes that were on the file-selection-id class that I need? What could be ways on how I could call the ID's of the checkboxes(for example if I had 4 checkboxes with 2 selections)?

Upvotes: 2

Views: 14643

Answers (1)

Use .map

var arr = $('input:checkbox.file-selection-id').map(function () {
    return this.id;
}).get();


if you want id of checked check-boxes

var arr = $('input:checkbox.file-selection-id').filter(':checked').map(function () {
    return this.id;
}).get();

var arr = $('input:checkbox.file-selection-id:checked').map(function () {
    return this.id;
}).get();


.filter()

Upvotes: 5

Related Questions