views:

165

answers:

2

I want to do an action when a user checks a checkbox, but I can't get it to work, what am I doing wrong?

So basically, a user goes to my page, ticks the box, then the alert pops up.

if($("#home").is(":checked"))
{
      alert('');
}
+6  A: 

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');
    }
});
Joel Potter
+1 for good comments and being faster than me.
Jeff Martin
Thanks for the comment Joel!
Keith Donegan
+2  A: 

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('');
    }
});
Jeff Martin
Thank you too Jeff
Keith Donegan