I want to check whether checkbox is checked or not using jquery. I want to check it on checkbox's onclick event.
<input type="checkbox" onclick="javascript:check_action();" id="Public(Web)" checked="checked" value="anyone" name="data[anyone]">
Is it possible? How?
Thanks.
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
When using jQuery and you wish to read whether a checkbox is checked or not. $('#checkboxid').is(':checked'); This will return true if the checkbox is checked and false if left unchecked.
The :checked selector works for checkboxes, radio buttons, and options of select elements. To retrieve only the selected options of select elements, use the :selected selector.
First, don't use javascript:
in event handler attributes. It's wrong and only works because it happens to be valid JavaScript syntax. Second, your id
is not valid. Parentheses are not allowed in the id
attribute (in HTML 4 at least; HTML 5 lifts this restriction). Third, if you're using jQuery it probably makes sense to use its click()
method to handle the click
event, although be aware that changing it to do that will mean that if the user clicks on the checkbox before the document has loaded then your script won't handle it.
<input type="checkbox" id="Public_Web" checked value="anyone"
name="data[anyone]">
$(document).ready(function() {
$("#Public_Web").click(function() {
if (this.checked) {
alert("Checked!");
}
});
});
You can also use this
if($("#checkbox_id").is(':checked'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With