samiles
samiles

Reputation: 3900

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!

Upvotes: 3

Views: 12231

Answers (7)

Simone
Simone

Reputation: 21262

Here is the modern JS way, without using jQuery:

Array.from(document.querySelectorAll('[type=checkbox]')).some(x => x.checked)

It will return true if at least one checkbox is checked, false otherwise.

Upvotes: 0

FiveTools
FiveTools

Reputation: 6030

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

or

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

Upvotes: 10

Ajay Jangra
Ajay Jangra

Reputation: 174

function checkbox(){
    var ischecked =$('input[type=checkbox]:checked').length;
    if (ischecked <= 0){        // you can change your condition
    alert("No Record Selected");
    return false;
    }else
    {
      -------//your Action that you want
    }

}

Upvotes: 0

Dennis
Dennis

Reputation: 14477

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;
}

Upvotes: 5

shanabus
shanabus

Reputation: 13115

This works:

var ischecked =$('input[type=checkbox]:checked').length;

so

if (ischecked > 0)

then at least one is checked.

Upvotes: 0

andi
andi

Reputation: 6522

if you only want to look at checkboxes, not radio buttons: var isChecked = $("input:checkbox").is(":checked");

if you're ok with checkboxes or radio buttons: var isChecked = $("input").is(":checked");

Upvotes: 0

xCRKx TyPHooN
xCRKx TyPHooN

Reputation: 808

if($('checkbox:checked').length > 0)
    //Some checkbox is checked

This should find any checkboxes on the page that are checked

Upvotes: 1

Related Questions