Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery checkbox event handling

I have the following:

<form id="myform">    <input type="checkbox" name="check1" value="check1">    <input type="checkbox" name="check2" value="check2"> </form> 

How do I use jQuery to capture any check event occuring in myform and tell which checkbox was toggled (and know if it was toggled on or off)?

like image 867
aeq Avatar asked Aug 09 '10 16:08

aeq


People also ask

How can create checkbox click event in jQuery?

change() updates the textbox value with the checkbox status. I use . click() to confirm the action on uncheck. If the user selects cancel, the checkmark is restored but .

How do I check if a checkbox is checked in an event?

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.

What is the event of checkbox?

Checkbox event handling using pure Javascript There are two events that you can attach to the checkbox to be fired once the checkbox value has been changed they are the onclick and the onchange events.

How do I listen to a checkbox?

Use the change() event, and the is() test: $('input:checkbox'). change( function(){ if ($(this).is(':checked')) { alert('checked'); } });


1 Answers

$('#myform :checkbox').change(function() {     // this will contain a reference to the checkbox        if (this.checked) {         // the checkbox is now checked      } else {         // the checkbox is now no longer checked     } }); 
like image 144
Darin Dimitrov Avatar answered Oct 04 '22 04:10

Darin Dimitrov