Posted By: richard_deeming | Jul 2nd, 2004 @ 5:18 AM
page 1 of 1
Comments: 3 | Views: 7881

According to the documentation, the getElementById function: "Returns the first object with the same ID attribute as the specified value." However, it actually returns the first element with the same name or id attribute as the value.

For example:


<html>
<head>
 <title>Test Description</title>
 <meta name="description" value="This is the meta-tag!" />
 
 <script language="javascript" type="text/javascript">
 <!--
 function testDescription()
 {
  var txt = document.getElementById("description");
  if (!txt)
  {
   alert("Not found!");
   return false;
  }
  else
  {
   alert(txt.value);
  }
 }
 //-->
 </script>
</head>
<body>
 <textarea id="description">This is the text area</textarea><br />
 <button onclick="testDescription();">Test</button>
</body>
</html>


In most browsers, the message will display: "This is the text area". In Internet Explorer, it will display, "This is the meta-tag!".

Yep, I've seen this before. Its a problem because some developers don't notice this as everything appears to work. Like you said, most browsers (e.g. Mozilla) deal with this correctly. I think there are a growing number of websites that fail in Mozilla because they rely on this bad "feature" of IE.

pyc
pyc
I think this is not a bug. In HTML 4.01 specification there is no specification for attribute called value for textarea element, however Firefox is supporting this attribute so it works in it. This is the example where IE behaves "by the book"... and FF not... I'm still on the side of FF Smiley

Anyways, textarea element is pretty stupid. I'm searching right now how can I change its value dynamically in IE with DOM's getElementById method ... Anyone have the solution?

Why the hell it had to be <textarea>....</textarea>  and not just <textarea value="something" /> ......
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
pyc wrote:
I think this is not a bug. In HTML 4.01 specification there is no specification for attribute called value for textarea element, however Firefox is supporting this attribute so it works in it. This is the example where IE behaves "by the book"... and FF not... I'm still on the side of FF

That isn't the bug he was talking about. He was talking about getElementById returning the <meta> element instead of the <textarea> element in IE because it considers name as well as id. That's definitely a bug.

pyc wrote:
Anyways, textarea element is pretty stupid. I'm searching right now how can I change its value dynamically in IE with DOM's getElementById method ... Anyone have the solution?

Why the hell it had to be <textarea>....</textarea>  and not just <textarea value="something" /> ......

It's because <textarea> is multiline, and attribute values can't have line breaks.

And despite the fact that textarea has no value attribute in HTML, it does have one in the DOM! See DOM Level 1 on the HTMLTextAreaElement interface. So if you want to change the value from script, you can use document.getElementById("textAreaId") = "Foo" without issues in any browser, and it'll be according to spec.

And for the record, it's actually <meta> that doesn't have a value attribute (it's supposed to be content), but that's beside the OP's point. Smiley

EDIT: Also, you're replying to a two year old post. Why?
page 1 of 1
Comments: 3 | Views: 7881