Bug Tracker

Ticket #1070 (closed bug: fixed)

Opened 2 years ago

Last modified 1 year ago

IE attr with value 0 BUG

Reported by: jakecigar Assigned to: brandon
Type: bug Priority: minor
Milestone: 1.2.2 Component: core
Version: 1.1.2 Keywords:
Cc: Needs: Commit

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?

Attachments

Change History

follow-up: ↓ 3   Changed 2 years ago by brandon

  • need changed from Review to Test Case

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

  Changed 2 years ago by brandon

  • priority changed from major to minor
  • component changed from ajax to core

in reply to: ↑ 1   Changed 2 years ago by jakecigar

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

  Changed 1 year ago by brandon

  • owner set to brandon
  • description changed from 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? to 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?

  Changed 1 year 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.

  Changed 1 year ago by owen

  • status changed from closed to reopened
  • resolution 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 "-//W3C//DTD XHTML 1.0 Strict//EN" "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 1 year 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. :)

  Changed 1 year 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.

Note: See TracTickets for help on using tickets.