Side navigation
#8699 closed bug (fixed)
Opened March 29, 2011 07:51PM UTC
Closed April 15, 2011 02:29AM UTC
.attr() returns -1 on missing attribute instead of undefined
Reported by: | anonymous | Owned by: | timmywil |
---|---|---|---|
Priority: | blocker | Milestone: | 1.6 |
Component: | attributes | Version: | 1.5.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
When element(s) are missing the attribute, attr works as follows.
$('elem').attr('noattr') == -1
This is a change from 1.4 where .attr() returned "undefined" or some other "falsy" response. Since -1 isn't considered falsy by javascript the following code is now broken.
if ($('elem').attr('noattr')) { doStuff();} else { doOtherStuff();}
If the intended attribute check is now -1 please update documentation so we can update code, or fix the regression that makes this fail a boolean check.
Attachments (0)
Change History (9)
Changed March 29, 2011 07:55PM UTC by comment:1
Changed March 29, 2011 08:30PM UTC by comment:2
i think i got this one right.
I'm loading jquery in the head, but the code that runs is in a document ready function.
Changed March 29, 2011 08:37PM UTC by comment:3
Replying to [comment:2 anonymous]:
i think i got this one right.
Meaning i've never used JSfiddle before, so i'm not 100% sure i replicated the test here correctly.
Changed March 29, 2011 08:38PM UTC by comment:4
Nice catch! You are correct and I never noticed that before.
Nevertheless, this has been fixed in the upcoming attrHooks updates for jQuery 1.6.
Changed March 29, 2011 08:43PM UTC by comment:5
Replying to [comment:4 timmywil]:
Nice catch! You are correct and I never noticed that before. Nevertheless, this has been fixed in the upcoming attrHooks updates for jQuery 1.6. http://jsfiddle.net/timmywil/uyLQL/9/
As long as I know it's fixed later on, I suppose I can wait. :P Back to vanilla dom for now i guess.
Changed March 29, 2011 08:51PM UTC by comment:6
If it helps, the way we retrieve maxLength in 1.6 is by using node.getAttribute('maxLength') rather than the property node.maxLength. Perhaps we could put a quick update for maxlength in 1.5.2, but since attr is getting a rewrite, my guess is that it's not the highest priority.
An example with raw js: http://jsfiddle.net/timmywil/uyLQL/10/
Changed March 30, 2011 03:51PM UTC by comment:7
component: | unfiled → attributes |
---|---|
milestone: | 1.next → 1.6 |
priority: | undecided → blocker |
status: | new → open |
Changed March 30, 2011 07:33PM UTC by comment:8
owner: | → timmywil |
---|---|
status: | open → assigned |
Changed April 15, 2011 02:29AM UTC by comment:9
resolution: | → fixed |
---|---|
status: | assigned → closed |
Please provide a test case on http://jsfiddle.net. Non-existing attributes should return undefined. http://jsfiddle.net/timmywil/2Ymea/