<html>
<head>
<title>IE document.getElementById bug</title>
<meta name="description" content="IE bugs" />
</head>
<body>
<textarea name="description" id="description">
This is information about the bug
</textarea>
<script type="text/javascript">
alert(document.getElementById('description').value);
</script>
</body>
</html>
okay,
If you view the example in Firefox, you will get a JavaScript alert message :
This is information about the bug
However, if you view it in IE7 the JavaScript alert will contain the word “undefined”.
The error is caused because IE’s document.getElementById(’description’) sees the meta tag with the name attribute set to “description” and since it treats name and id attributes as interchangeable, returns the meta tag instead of the textarea which actually has an id set to “description”.
JavaScript programmers who are familiar with this bug often take great care to avoid the problem by being circumspect with the names and ids of their elements. However, this becomes increasingly difficult as applications become more complex with multiple reusable parts that are included into various parts of the same system, especially with multiple programmers working concurrently. Naming conventions can help, but there are times when the id of an input element (which must be unique) differs from its name attribute, which does not have to be unique.
How to solve it,
You can change the textarea name . May be change to "desc"
Other Solution or more details can take a look on this post:
http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
Great Post. Even i was facing the same problem thanks for the update.
ReplyDelete