I have this problem with IE 10. I have this code inside a form in an HTML page:
<input type='hidden' name='X123' />
<input type='hidden' name='123' />
<input type='hidden' name='00123' />
If I run this JS command:
document.forms['MyForm'].elements[name];
where name
are the names of the inputs, I can get only the first ('X123'
). In the other two cases the function returns undefined
. I tested it on IE 10 and Firefox. On Firefox works. However, it has to run under IE. This is a MWE:
<html><head><script type='text/javascript'>
function println(text) {
document.getElementById('output').innerHTML += '<p>' + text + '</p>';
}
function test() {
println('X123 = ' + document.forms['MyForm'].elements['X123']);
println('123 = ' + document.forms['MyForm'].elements['123']);
println('00123 = ' + document.forms['MyForm'].elements['00123']);
}
</script></head><body onload='test();'><form name='MyForm'>
<input type='hidden' name='X123' />
<input type='hidden' name='123' />
<input type='hidden' name='00123' />
</form><div id='output'></div></body></html>
I need to know if this is a known behavior of IE.
A better explanation: I bump into this problem at work. I made the HTML you see just to replicate the problem (it is not a tutorial). I know I can use jQuery or other methods to get the elements by name, but I need to know if this is a known behavior of IE because I can't find anything about on the net.
Not just IE; that code doesn't work on Chrome or Firefox either, for the
123
element. Grundy seems to have found the relevant info about why, which is that all-digits names cause confusion with index-based access, and so aren't available by name.Yes, you can use
querySelector
instead (available on all modern browsers, and also IE8). I don't have IE10 handy, but it works for me on IE8, IE9, IE11, Chrome, and Firefox (the original code doesn't work in any of them) so I'd say it almost certainly works on IE10 as well:Note that the quotes around the values in
[name="value"]
are important, because otherwise the ones starting with digits are a CSS syntax error.Live Example:
And if you want their values: