jQuery get array of checked checkboxes ids

Share this article

Use jQuery get array of checked checkboxes ids.

var checkedIds = Array();
$('form:input[type="checkbox"][checked]').each( function(i,v)
{
    checkedIds.push($(v).attr('id'));
});
console.log(checkedIds);

Frequently Asked Questions (FAQs) about jQuery and Checkbox IDs

How can I select all checkboxes using jQuery?

To select all checkboxes using jQuery, you can use the “:checkbox” selector. This selector will select all elements of type checkbox. Here is an example:

$('input:checkbox').prop('checked', true);
This code will select all checkboxes and set their property ‘checked’ to true, effectively checking all checkboxes.

How can I uncheck all checkboxes using jQuery?

Similar to the previous question, you can use the “:checkbox” selector to select all checkboxes. To uncheck all checkboxes, you can set the ‘checked’ property to false. Here is an example:

$('input:checkbox').prop('checked', false);
This code will uncheck all checkboxes.

How can I get the value of a checked checkbox using jQuery?

To get the value of a checked checkbox, you can use the “:checked” selector. This selector will select all checked checkboxes. Here is an example:

$('input:checkbox:checked').val();
This code will return the value of the first checked checkbox it finds.

How can I get the IDs of all checked checkboxes using jQuery?

To get the IDs of all checked checkboxes, you can use the “:checked” selector along with the .map() function. Here is an example:

var ids = $('input:checkbox:checked').map(function() {
return this.id;
}).get();
This code will return an array of IDs of all checked checkboxes.

How can I check a checkbox by its ID using jQuery?

To check a checkbox by its ID, you can use the “#” selector followed by the ID of the checkbox. Here is an example:

$('#myCheckbox').prop('checked', true);
This code will check the checkbox with the ID ‘myCheckbox’.

How can I uncheck a checkbox by its ID using jQuery?

Similar to the previous question, you can use the “#” selector followed by the ID of the checkbox to uncheck it. Here is an example:

$('#myCheckbox').prop('checked', false);
This code will uncheck the checkbox with the ID ‘myCheckbox’.

How can I toggle the checked state of a checkbox using jQuery?

To toggle the checked state of a checkbox, you can use the .click() function. Here is an example:

$('#myCheckbox').click();
This code will toggle the checked state of the checkbox with the ID ‘myCheckbox’.

How can I check if a checkbox is checked using jQuery?

To check if a checkbox is checked, you can use the .is() function along with the “:checked” selector. Here is an example:

if ($('#myCheckbox').is(':checked')) {
// Checkbox is checked
} else {
// Checkbox is not checked
}
This code will check if the checkbox with the ID ‘myCheckbox’ is checked.

How can I handle the change event of a checkbox using jQuery?

To handle the change event of a checkbox, you can use the .change() function. Here is an example:

$('#myCheckbox').change(function() {
if ($(this).is(':checked')) {
// Checkbox is checked
} else {
// Checkbox is not checked
}
});
This code will handle the change event of the checkbox with the ID ‘myCheckbox’.

How can I select multiple checkboxes by their IDs using jQuery?

To select multiple checkboxes by their IDs, you can use the “#” selector followed by the IDs of the checkboxes. Here is an example:

$('#myCheckbox1, #myCheckbox2, #myCheckbox3').prop('checked', true);
This code will check the checkboxes with the IDs ‘myCheckbox1’, ‘myCheckbox2’, and ‘myCheckbox3’.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form