How to do an action when a checkbox is checked with jQuery?
What you are looking for is called an Event. JQuery provides simple event binding methods like so
$("#home").click(function() {
// this function will get executed every time the #home element is clicked (or tab-spacebar changed)
if($(this).is(":checked")) // "this" refers to the element that fired the event
{
alert('home is checked');
}
});
you need to use the .click event described here: http://docs.jquery.com/Events/click#fn
so
$("#home").click( function () {
if($("#home").is(":checked"))
{
alert('');
}
});
Actually the change()
function is much better for this solution because it works for javascript generated actions, such as selecting every checkbox via a script.
$('#home').change(function() {
if ($(this).is(':checked')) {
...
} else {
...
}
});