Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange jQuery("checkbox").click().is(":checked") behavior

Can anyone explain why $(this).is(":checked") gives the opposite result when the checkbox is clicked with $("#test").click() than when clicked manually or with document.getElementById("test").click() ??

EDIT Requested behavior here - thanks:

http://jsfiddle.net/ub8Zk/4/

EDIT 2

This has been driving me nuts, but I finally realize -- in version 1.5.2 of jQuery the event handler for the change event is fired when click() method is called (like native js)!! Not so in previous versions.

Look here:

http://dl.dropbox.com/u/6996564/jquery_click_test/test-1.4.4.htm ... test-1.5.1.htm ... test-1.5.2.htm

Can someone help me report this bug??

like image 803
dalgard Avatar asked Jan 21 '23 00:01

dalgard


2 Answers

The click event happens BEFORE the value changes, so it is getting the old value. The the default handler for click happens AFTER your click event and toggles the value. That is why it is getting the opposite value. I would think the document click function is doing something wierd (I would not trust it, I would trust jQuery).

Look at this fiddle: http://jsfiddle.net/ub8Zk/4/

like image 171
Bob Fincheimer Avatar answered Jan 29 '23 08:01

Bob Fincheimer


Since you are using a checkbox input, you want is(':checked') not is(':selected')

like image 26
Hussein Avatar answered Jan 29 '23 08:01

Hussein