Skip to main content

Bug Tracker

Side navigation

#9856 closed bug (worksforme)

Opened July 19, 2011 01:07AM UTC

Closed July 19, 2011 05:45AM UTC

Last modified July 22, 2011 10:40AM UTC

Checkbox checked returning value or undefined in 1.6.2 not true or false.

Reported by: Glenn Owned by:
Priority: low Milestone: None
Component: attributes Version: 1.6.2
Keywords: Cc:
Blocked by: Blocking:
Description

Recently upgraded from jQuery 1.4.4 to 1.6.2 and noticed (because code was breaking) that when you alert the value of $('#myID').attr('checked') if the checkbox was checked you get back 'checkbox'. If it was not checked you get back 'undefined'.

I checked other versions of jquery and this doesn't happen until the change from 1.5.2 -> 1.6.0, where the move to prop() made them both return 'undefined'.

1.6.1 is where we start seeing value or undefined returned.

Shouldn't this be returning true or false if the goal is backwards compatibility?

Here is an example of the values in 1.5.2 http://jsfiddle.net/Gv3bW/

and in 1.6.2 http://jsfiddle.net/6R2df/

The code that would need to change to make them return true / false is in boolHooks.get, line 2402 of http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js.

From:

return jQuery.prop( elem, name ) ? name.toLowerCase() : undefined;

To:

return !!jQuery.prop( elem, name );
Attachments (0)
Change History (5)

Changed July 19, 2011 05:45AM UTC by addyosmani comment:1

component: unfiledattributes
priority: undecidedlow
resolution: → worksforme
status: newclosed

Please read here for the changes that were introduced in 1.6.x:

http://blog.jquery.com/2011/05/03/jquery-16-released/

You may note that there were some significant changes introduced with respect to how attributes are handled, in particular that a new .prop() method was added.

With this in mind, .attr(N) will no longer return the checked value if one isn't supplied. Instead, you should use .prop(N) for this as I've demonstrated in this updated version of your 1.6.2 test case: http://jsfiddle.net/6R2df/1/.

Thanks!

Changed July 19, 2011 05:55AM UTC by anonymous comment:2

Thanks for the quick response. I know about .prop() and have already updated my site to use it.

My ticket is about .attr() and the code that was added to make it backwards compatible and the comments that were made about it in the release notes for 1.6.1, specifically:

"However, once jQuery 1.6 was released... Therefore, in the interest of backwards compatibility and the usefulness of the .attr() method, we will continue to be able to get and set these boolean attributes with the .attr() method in jQuery 1.6.1."

I don't see how returning different things between the different versions maintains that compatibility.

Not a massive issue for me that my site is now fixed but I just thought it incorrect as I did have to change to prop() to get my site to work with 1.6.2.

Changed July 19, 2011 01:31PM UTC by timmywil comment:3

It's true the returns values are not the same as they used to be. However, returning a boolean did not make sense while prop exists. Attribute values should be a string or undefined while the property value is a boolean. The suggested way to check if a checkbox is checked has always been to check for a truthy value. For instance:

if ( $('input:checkbox').attr('checked') ) {}

instead of

if ( $('input:checkbox').attr('checked') === true ) {}

Any code that was written in the recommended way would not have to be changed.

Changed July 20, 2011 01:00AM UTC by anonymous comment:4

Cool. Thanks for the reply.

Changed July 22, 2011 10:40AM UTC by addyosmani comment:5

#9890 is a duplicate of this ticket.