Handling HTML Checkboxes. Execute something when checked/unchecked.

In some cases, you are required to do something (EG: show/hide divs) when a checkbox is clicked. Here is an example using jQuery.

First your checkbox will look like this:

...html code here...

< input name="myCheckBox" id="myCheckBox" value="yes" type="checkbox" /> This is a sample checkbox.

Then somewhere in your HTML head, your Javascript code will look like this:

...other code here...
 
    $('#myCheckBox').click(function(){
        if (this.checked) {      //this.checked is NOT from jquery
            alert('Do any action here.');
        } else {
            alert('Undo the action here.');
        }
    });
 
...other code here...

NOTE: Do not forget to download and then include the required jQuery files in your HTML File.


Subscribe to comments Comment | Trackback |
Post Tags: ,

Browse Timeline


Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> <pre lang="" line="">


© Copyright 2007 eKini: Web Developer Blog . Thanks for visiting!