A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

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...
        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.

This entry was posted in General and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>