#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: | ||
Blocked by: | Blocking: |
Description (last modified by )
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 (9)
comment:1 follow-up: 3 Changed 16 years ago by
need: | Review → Test Case |
---|
comment:2 Changed 16 years ago by
Component: | ajax → core |
---|---|
Priority: | major → minor |
comment:3 Changed 16 years ago by
Replying to brandon: the value of selection is 0 (zero)... that's the problem! other values work!
comment:4 Changed 16 years ago by
Description: | modified (diff) |
---|---|
Owner: | set to brandon |
comment:5 Changed 16 years ago by
Resolution: | → worksforme |
---|---|
Status: | new → closed |
I can't reproduce this. Feel free to reopen with a test case.
comment:6 Changed 15 years ago by
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
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 15 years ago by
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 15 years ago by
Milestone: | 1.1.3 → 1.2.2 |
---|---|
need: | Test Case → Commit |
Resolution: | → fixed |
Status: | reopened → closed |
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.
What is the value of selection? Could you possibly attach a test case?