Bug Tracker

Modify

Ticket #9856 (closed bug: worksforme)

Opened 23 months ago

Last modified 22 months ago

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:
Blocking: Blocked by:

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

comment:1 Changed 23 months ago by addyosmani

  • Priority changed from undecided to low
  • Resolution set to worksforme
  • Status changed from new to closed
  • Component changed from unfiled to attributes

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 23 months 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 23 months 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 22 months ago by anonymous

Cool. Thanks for the reply.

comment:5 Changed 22 months ago by addyosmani

#9890 is a duplicate of this ticket.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.