Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 10 years ago

#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 brandon)

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 Changed 13 years ago by brandon

need: ReviewTest Case

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

comment:2 Changed 13 years ago by brandon

Component: ajaxcore
Priority: majorminor

comment:3 in reply to:  1 Changed 13 years ago by jakecigar

Replying to brandon: the value of selection is 0 (zero)... that's the problem! other values work!

comment:4 Changed 13 years ago by brandon

Description: modified (diff)
Owner: set to brandon

comment:5 Changed 13 years ago by brandon

Resolution: worksforme
Status: newclosed

I can't reproduce this. Feel free to reopen with a test case.

comment:6 Changed 13 years ago by owen

Resolution: worksforme
Status: closedreopened

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 13 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.

I can think of two possible ways to fix this problem depending why the
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 13 years ago by davidserduke

Milestone: 1.1.31.2.2
need: Test CaseCommit
Resolution: fixed
Status: reopenedclosed

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.

comment:9 Changed 10 years ago by othmane126@…

It Doesn't work even with FireFox 3.6.13 !!!!

Note: See TracTickets for help on using tickets.