Skip to main content

Bug Tracker

Side navigation

#9277 closed bug (wontfix)

Opened May 13, 2011 07:23PM UTC

Closed May 13, 2011 07:39PM UTC

'undefined' from .attr() causes potential breaking chaining issues in overload setter/getter

Reported by: mr.leon.yu@gmail.com Owned by: mr.leon.yu@gmail.com
Priority: undecided Milestone: 1.next
Component: attributes Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:
Description

With advent of jQuery 1.6, which returns undefined in .attr(), the occurrence of undefined in jQuery code has become ever more prevalent. This can cause a lot more existing code to break. The for example the following code:

$('element').attr('title', $('element2').attr('alt')).css('color', '#000');

In the above code, if $('element2').attr('alt') returns a value, the code would run fine. However, in 1.6, when $('element2').attr('alt') is undefined, the line would break because it would turn the setter call into an getter call. In turn the getter call would stop the chaining from working.

Solution:

Make sure arguments.length == 1 before assuming the call is a getter call.

Attachments (0)
Change History (3)

Changed May 13, 2011 07:29PM UTC by ajpiano comment:1

component: unfiledattributes
status: newopen

Even before 1.6, the fact that a setter would act like a getter if the value you passed as the second argument was unexpectedly undefined was... unexpected and confusing to users. I concur with the OP that we should probably check arguments.length here.

Changed May 13, 2011 07:35PM UTC by rwaldron comment:2

owner: → mr.leon.yu@gmail.com
status: openpending

I agree that this should be handled more like other user programming errors (quietly, discretely) but it still stands that if you know there is a possibility of that attr call returning undefined, you should be handling it in your own code. it's fixable with four characters:

$('element').attr('title', $('element2').attr('alt') || '' ).css('color', '#000');

Changed May 13, 2011 07:39PM UTC by rwaldron comment:3

resolution: → wontfix
status: pendingclosed

A further look at the history of the test suite reveals _many_ tests written to specifically support this expectation