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.
Check if one item or more item is checked under Doctor.
document.querySelectorAll('#doctor input[type=checkbox]:checked').length > 0
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');
}
orv-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
Jquery Ajax Call to PHP Script with JSON Return
How to Hide Form Code from View Code/Inspect Element Browser
Removing # from Url in Angularjs While Having .Run in Routes
How to Get Jquery.Trigger('Click'); to Initiate a Mouse Click
How to Use Window.Postmessage Across Domains
How to Enable Cors Nodejs with Express
Is This an Example of Variable Shadowing in JavaScript
Is There Ever a Good Reason to Pass a String to Settimeout
Foreach on Queryselectorall Not Working in Recent Microsoft Browsers
Axios Posting Params Not Read by $_Post
How to Replace Dom Element in Place Using JavaScript
Msie and Addeventlistener Problem in JavaScript