Question Outline
I have an HTML page with JavaScript code within, generated via PHP. Normally, the elements["name"]
bracket notation works properly, and I can access the named element this way. However, after several maneuvers through the pages, the same page will act up, and random elements["name"]
calls will turn up as undefined
... Checking the HTML source and DOM Explorer, the corresponding HTML elements are still there, so it seems the JavaScript side is somehow unable to access the elements... And even stranger, an elements.namedItem("name")
call to the same elements in the same condition works for some reason...
These two calls should be equivalent, according to this MDN reference: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection#Usage_in_JavaScript
I am unable to find either a clear reproducible pattern, or a good reason behind this behavior, and so am thinking that I use the latter call to be safe, but am grateful for any documentary reasoning behind this failure... if any such exist.
Environment
Internet Explorer 11 running in IE modes 5 to 8 runs into this problem; strangely, the same Internet Explorer 11 running in IE modes 9, 10, or Edge do not show this problem for some reason or another...
Codes
JavaScript snippet
formel=document.forms.MyTestForm.elements;
el=document.getElementById("errorlogarea");
//formel.type[27] won't work because of the square brackets...
//formel["type[27]"] turns out to be undefined...?
if(formel["type[27]"]!=undefined) el.innerHTML+="t27-1:"+formel["type[27]"].value+"<br>";
//...but formel.namedItem("type[27]") correctly gets the (hidden) input...???
if(formel.namedItem("type[27]")!=undefined) el.innerHTML+="t27-2:"+formel.namedItem("type[27]").value+"<br>";
HTML snippet
<form id="MyTestForm" action="(this-page)" method="POST" onSubmit="return CheckBeforeSubmit();">
...
<div class="errorlog" id="errorlogarea"></div><br>
...
<input type="hidden" name="type[27]" value="any">
...
</form>