Handling HTML Checkboxes. Execute something when checked/unchecked.
November23
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.