Ticket #1070 (closed bug: fixed)
IE attr with value 0 BUG
| Reported by: | jakecigar | Owned by: | brandon |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.2.2 |
| Component: | core | Version: | 1.1.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by brandon) (diff)
1555 if ( value != undefined ) elem.setAttribute( name, value );
in jquery.js
sets a value of number 0, which IE thinks it means to delete the attribute.
for instance
img.attr({selection:""+selection,src:imgs[selection],title:values[selection]})
works in all browsers
but
img.attr({selection:selection,src:imgs[selection],title:values[selection]})
does not work in IE.
Do you see this as a bug or a feature?
Change History
comment:2 Changed 6 years ago by brandon
- Priority changed from major to minor
- Component changed from ajax to core
comment:3 in reply to: ↑ 1 Changed 6 years ago by jakecigar
Replying to brandon: the value of selection is 0 (zero)... that's the problem! other values work!
comment:5 Changed 6 years ago by brandon
- Status changed from new to closed
- Resolution set to worksforme
I can't reproduce this. Feel free to reopen with a test case.
comment:6 Changed 6 years ago by owen
- Status changed from closed to reopened
- Resolution worksforme deleted
I have come across what seems like the same bug. Here is a test case. Works as expected in FF but both IE6 and IE7 return 'undefined' when a value of 0 is set. Try the code below in IE to see what I mean, comparing with FF if necessary.
<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 StrictEN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xml:lang="en-UK" lang="en-UK" xmlns=" http://www.w3.org/1999/xhtml">
<head> <title>jquery bug test</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <script type="text/javascript" src="jquery-nightly.js"></script> </head>
<body>
<div id="test"></div>
<script type="text/javascript"> <![CDATA[
$("#test").attr("someAttr", "0");
alert("setting attribute to the string '0' we get: " + $("#test").attr("someAttr"));
$("#test").attr("someAttr", 0);
alert("setting attribute to the number 0 we get: " + $("#test").attr("someAttr"));
$("#test").attr("someAttr", 1);
alert("setting attribute to the number 1 we get: " + $("#test").attr("someAttr"));
]]> </script>
</body> </html>
comment:7 Changed 6 years ago by davidserduke
This appears to be caused in function jQuery.fn.attr() by the line
return this.length && jQuery[ type || "attr" ]( this[0], key ) || undefined;
It looks like in FF when you set an attribute with a number it gets converted to a string. But in IE, it stays a number. So when you set
$('#test').attr("someAttr", 0);
it returns in FF: "0" it returns in IE: 0
So the line I mentioned above is false because of the coercion involved when IE returns 0 thus it returns undefined.
| undefined part is there. The first would be to remove it and just have: |
return this.length && jQuery[ type || "attr" ]( this[0], key );
The other method would be to change the link almost right below it that says
obj[ key ] = value;
To
obj[ key ] = "" + value;
Both fixes have different possible side effects. The first could end up returning "" or null instead of undefined depending on the browser implementation. The second could be a problem in IE if sometimes it is required to pass in a number instead of a string.
So I'll give this info and leave the actual fix up to the committers in charge. :)
comment:8 Changed 5 years ago by davidserduke
- need changed from Test Case to Commit
- Status changed from reopened to closed
- Resolution set to fixed
- Milestone changed from 1.1.3 to 1.2.2
Fixed in [3971]. On further examination it appeared the best place to fix this was right at the setAttribute() call. setAttribute() expected 2 strings and getAttribute() was supposed to return a string (according to JS: TDG). In IE, the string conversion wasn't happening so the fix just forced it which brought all the browsers in line together.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

What is the value of selection? Could you possibly attach a test case?