Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check whether checkbox is checked or not using jquery

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.

like image 897
gautamlakum Avatar asked Oct 27 '10 10:10

gautamlakum


People also ask

How do you check if a checkbox is checked or not in jQuery?

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.

How do you check if the checkbox is checked or not?

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.

How checkbox is checked true in jQuery?

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.

Is checked function in jQuery?

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.


2 Answers

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!");
        }
    });
});
like image 191
Tim Down Avatar answered Sep 28 '22 04:09

Tim Down


You can also use this

if($("#checkbox_id").is(':checked'))
like image 23
Edson Medina Avatar answered Sep 28 '22 04:09

Edson Medina