Skip to main content

Bug Tracker

Side navigation

#1070 closed bug (fixed)

Opened March 25, 2007 08:37PM UTC

Closed November 28, 2007 11:17PM UTC

Last modified December 17, 2010 04:03PM UTC

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

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?

Attachments (0)
Change History (9)

Changed April 03, 2007 10:53PM UTC by brandon comment:1

need: ReviewTest Case

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

Changed April 03, 2007 10:53PM UTC by brandon comment:2

component: ajaxcore
priority: majorminor

Changed April 25, 2007 05:59PM UTC by jakecigar comment:3

Replying to [comment:1 brandon]:

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

Changed July 20, 2007 09:31PM UTC by brandon comment:4

description: 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?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?
owner: → brandon

Changed July 21, 2007 02:08AM UTC by brandon comment:5

resolution: → worksforme
status: newclosed

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

Changed September 19, 2007 02:53PM UTC by owen comment:6

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>

Changed October 04, 2007 01:44AM UTC by davidserduke comment:7

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. :)

Changed November 28, 2007 11:17PM UTC by davidserduke comment:8

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.

Changed December 17, 2010 04:03PM UTC by othmane126@hotmail.com comment:9

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