Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if any checkboxes at all on the page are checked?

I need to check, using jQuery, if any checkboxes on the page are checked. The particular checkbox set they are contained in doesn't matter, just any of them.

Any ideas?

Thanks!

like image 531
samiles Avatar asked Dec 12 '25 06:12

samiles


2 Answers

var isChecked = $("input[type=checkbox]").is(":checked");

or

var isChecked = $('input:checkbox').is(':checked')

like image 116
FiveTools Avatar answered Dec 14 '25 20:12

FiveTools


The following function returns true if there are any ticked checkboxes and false otherwise.

function anyCheckbox()
{
    var inputElements = document.getElementsByTagName("input");
    for (var i = 0; i < inputElements.length; i++)
        if (inputElements[i].type == "checkbox")
            if (inputElements[i].checked)
                return true;
    return false;
}
like image 25
Dennis Avatar answered Dec 14 '25 19:12

Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!