views:

1736

answers:

4

The title should be clear

+1  A: 

use the click event for best compatibility with MSIE

$(document).ready(function() {
    $("input[type=checkbox]").click(function() {
        alert("state changed");
    });
});
Ty W
How to judge if it's checked on or not?
Shore
I need to know whether a click mean to check or uncheck it.
Shore
you can then use $(this).is(':checked') to test the state of the just-clicked item
Ty W
+2  A: 
<input type="checkbox" id="something" />

$("#something").click( function(){
   if( $(this).is(':checked') ) alert("checked");
});
marcgg
document.getElementById('something').checked works,why $('#something').checked not work?
Shore
Your code is missing a paran, and really should use the jQuery way of getting the check state.
Pete Michaud
Because $('#something') returns a jQuery object not an ElementNode
Dmitri Farkov
Shore: the reason getElementById works is that it returns a DOM element, whereas the $ jQuery function returns a jQuery object. The jQuery object has the DOM element as a property, but it is not, itself, a DOM object.
Pete Michaud
@Pete Michaud ,clear:)
Shore
@marcgg,why this.checked not working?
Shore
this.checked should work... try making an alert(this) an check that you have the correct element.
marcgg
+2  A: 

Use the :checked selector to determine the checkbox's state:

$('input[type=checkbox]').click(function() {
    if($(this).is(':checked')) {
        ...
    } else {
        ...
    }
});
karim79
need another parenthesis in your condition... if($(this)...
Ty W
@Ty W - thanks, fixed.
karim79
A: 
santosh
Hey!buddy,what are you doing here?
Shore