0

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>
Alex
  • 11,115
  • 12
  • 51
  • 64
yybtcbk
  • 3
  • 3
  • About the `I am unable to find either a clear reproducible pattern`, you write that IE 11 in 5 to 8 mode has that problem, but never (?) in the 9 to 11 mode. Does the behavior in IE in the 5 to 8 mode change when reloading, or just for other elements? – t.niese Mar 16 '15 at 08:30
  • Have you declared a `!DOCTYPE` ? IE<=8 requires a doctype to be defined, in order to target attribute-selectors. – Eric Mar 16 '15 at 09:55
  • @t.niese : Once the behavior happens, the elements which act up are constant, even after reloading the page (even in different IE modes). Upon restarting IE itself, the problems seems fixed for a while, and then reappears, sometimes with the same elements, sometimes different... which is all the more puzzling... – yybtcbk Mar 17 '15 at 01:39
  • @Eric : on the pages there is a combined ` ` and also a ``, which in combination makes IE work in IE5 mode by default. – yybtcbk Mar 17 '15 at 01:42

0 Answers0