How to Determine If a Checkbox Is Checked

Check if checkbox is checked with jQuery

IDs must be unique in your document, meaning that you shouldn't do this:

<input type="checkbox" name="chk[]" id="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" id="chk[]" value="Bananas" />

Instead, drop the ID, and then select them by name, or by a containing element:

<fieldset id="checkArray">
<input type="checkbox" name="chk[]" value="Apples" />

<input type="checkbox" name="chk[]" value="Bananas" />
</fieldset>

And now the jQuery:

var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;
//there should be no space between identifier and selector

// or, without the container:

var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;

How do I check whether a checkbox is checked in jQuery?

This worked for me:

$get("isAgeSelected ").checked == true

Where isAgeSelected is the id of the control.

Also, @karim79's answer works fine. I am not sure what I missed at the time I tested it.

Note, this is answer uses Microsoft Ajax, not jQuery

If checkbox is checked condition Javascript

I'm not sure what was going on but when I had if(checkbox == true) I was getting the message saying true is not defined as if it was looking for a variable called true.

I changed it to using Jquery like so if($("#checkbox").is(':checked'))

Dynamically check if the checkbox is checked on-fly

You can use the querySelectorAll function to get list of checked checkbox in specific div.

If size of list > 0, it contains checked checkbox.

  1. Check if one item or more item is checked under Doctor.

    document.querySelectorAll('#doctor input[type=checkbox]:checked').length > 0

  2. Check if one item or more item is checked under patient.

    document.querySelectorAll('#patient input[type=checkbox]:checked').length > 0

jQuery if checkbox is checked

if ($('input.checkbox_check').is(':checked')) {

How to determine whether a checkbox is checked or not in Vue js

You can do something like:

if(this.rolesSelected != "") {
alert('isSelected');
}

or
v-on:click="samplefunction({{$role->id}},$event)"

samplefunction : function(value,event) {
if (event.target.checked) {
alert('isSelected');
}
}

checking if a checkbox is checked?

if($('#element').is(':checked')){

//checkbox is checked

}

or

if($('#element:checked').length > 0){

//checkbox is checked

}

or in jQuery 1.6+:

if($('#element:checked').prop('checked') === true){

//checkbox is checked

}


Related Topics



Leave a reply



Submit