Last week I was stuck with javascript issue in IE. I have list of elements in a form and I want to access one of them by their name. In FF its works but in IE it doesn't work for me. It screwed me for an hour but finally I got the reason for the problem.
Let’s say I have an input box with name "1_txt" and a button with name "btn". When click the button it calls a function that will put a "Welcome!" message in the textbox. It works in FF. But in IE it changed the value of the button to "Welcome!" instead of the textbox.
Reason for the problem here is when we give element name start with number (1, 2, 3...) and followed by underscore ("_") and then alphanumeric. IE split the element name by underscore like 1+’_’+"txt". So it gets 1 as an index in the elements array. That’s why it updating the button (second item in the element array i.e., index is 1) element.
Eg:
<html>
<head>
<script language="javascript">
function showMessage()
{
document.forms["frm"].elements["1_txt"].value = "Welcome!";
}
</script>
</head>
<body>
<form name="frm">
<input type="text" name="1_txt" size="20" />
<input type="button" name="btn" value="Click" onclick="showMessage();" />
</form>
</body>
</html>
No comments:
Post a Comment