Check If Checkbox Is Checked With Jquery

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

jQuery if checkbox is checked

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

Check if 'this' checkbox is checked

What about this?

if($(this).is(':checked'))

jQuery: Test if checkbox is NOT checked

One reliable way I use is:

if($("#checkSurfaceEnvironment-1").prop('checked') == true){
//do something
}

If you want to iterate over checked elements use the parent element

$("#parentId").find("checkbox").each(function(){
if ($(this).prop('checked')==true){
//do something
}
});

More info:

This works well because all checkboxes have a property checked which stores the actual state of the checkbox. If you wish you can inspect the page and try to check and uncheck a checkbox, and you will notice the attribute "checked" (if present) will remain the same. This attribute only represents the initial state of the checkbox, and not the current state. The current state is stored in the property checked of the dom element for that checkbox.

See Properties and Attributes in HTML

jQuery, check if checkbox is checked with the same class

As of now you are checking any checkbox is checked. the condition needs to be changed.

You need to check the :checked state of the chechbox in current row while iterating.

if($(this).find(':checkbox:checked').length > 0){
//your existing code
}

jQuery or js check if checkbox is checked

Your code work on page load but if you want to run it on check/uncheck of checkbox, you should put it in onchange event handler of checkbox

$("#isgdpr").change(function(){
if ($(this).is(':checked'))
alert('Checked!');
});

$("#isgdpr").change(function(){  if ($(this).is(':checked'))    console.log('Checked!');});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label class="switch">  <input type="checkbox" id="isgdpr"></label>

Setting checked for a checkbox with jQuery

Modern jQuery

Use .prop():

$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);

DOM API

If you're working with just one element, you can always just access the underlying HTMLInputElement and modify its .checked property:

$('.myCheckbox')[0].checked = true;
$('.myCheckbox')[0].checked = false;

The benefit to using the .prop() and .attr() methods instead of this is that they will operate on all matched elements.

jQuery 1.5.x and below

The .prop() method is not available, so you need to use .attr().

$('.myCheckbox').attr('checked', true);
$('.myCheckbox').attr('checked', false);

Note that this is the approach used by jQuery's unit tests prior to version 1.6 and is preferable to using $('.myCheckbox').removeAttr('checked'); since the latter will, if the box was initially checked, change the behaviour of a call to .reset() on any form that contains it – a subtle but probably unwelcome behaviour change.

For more context, some incomplete discussion of the changes to the handling of the checked attribute/property in the transition from 1.5.x to 1.6 can be found in the version 1.6 release notes and the Attributes vs. Properties section of the .prop() documentation.

Check if checkbox is NOT checked on click - jQuery

Try this:

if(!$(this).is(':checked'))

demo

How to check if a checkbox is checked in contact form 7?

Your jQuery is incorrect for usage on WordPress. You can insert it into the CF7 Editor, but I would recommend minifying it so that cf7 doesn't auto-p it.

You can't just use $ in Wordpress.

<script>
jQuery(function($) {
$('#katoikia').on('change', function() {
// CHANGED THIS SELECTOR
if ($('#katoikia').is(':checked')) {
alert('Checked');
} else {
alert('Unchecked!');
}
});
});
</script>

Minified it would look like this:

<script>jQuery(function(e){e("#katoikia").on("change",function(){e("#katoikia").is(":checked")?alert("Checked"):alert("Unchecked!")})});</script>


Related Topics



Leave a reply



Submit