Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#9856 closed bug (worksforme)

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 );

Change History (5)

comment:1 Changed 8 years ago by addyosmani

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!

comment:2 Changed 8 years ago by anonymous

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.

comment:3 Changed 8 years ago by timmywil

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.

comment:4 Changed 8 years ago by anonymous

Cool. Thanks for the reply.

comment:5 Changed 8 years ago by addyosmani

#9890 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.