Opened 12 years ago
Closed 12 years ago
#9277 closed bug (wontfix)
'undefined' from .attr() causes potential breaking chaining issues in overload setter/getter
Reported by: | Owned by: | ||
---|---|---|---|
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.
Change History (3)
comment:1 Changed 12 years ago by
Component: | unfiled → attributes |
---|---|
Status: | new → open |
comment:2 Changed 12 years ago by
Owner: | set to [email protected]… |
---|---|
Status: | open → pending |
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');
comment:3 Changed 12 years ago by
Resolution: | → wontfix |
---|---|
Status: | pending → closed |
A further look at the history of the test suite reveals _many_ tests written to specifically support this expectation
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.