I want to count how many checkboxes on my page are selected using jQuery. I've written the following code:
var numberOfCheckboxesSelected = 0;
$(':checkbox').each(function(checkbox) {
if (checkbox.attr('checked'))
numberOfCheckboxesSelected++;
});
However, my code errors out with the message "Object doesn't support this property or method" on the third line.
How do I count how many checkboxes are selected on my page?
We can count the total number of checkboxes in a page in Selenium with the help of find_elements method. While working on any checkboxes, we will always find an attribute type in the html code and its value should be checkbox.
Using jQuery is(':checked')change(function(ev) { if ( $(this).is(':checked') ) console. log('checked'); else console. log('not checked'); }); If multiple checkboxes match the jQuery selector, this method can be used to check if at least one is checked.
jQuery supports the :checked
pseudo-selector.
var n = $("input:checked").length;
This will work for radio buttons as well as checkboxes. If you just want checkboxes, but also have radio buttons on the page:
var n = $("input:checkbox:checked").length;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With