Mark Evans Mark Evans - 2 years ago 76
HTML Question

document.getElementsByTagName("input"); returning length 0

HTML

Destination: @Html.TextBox("StudentName", null, new { @class = "form-control", @id="d" })
Leaving From @Html.TextBox("StudentName", null, new { @class = "form-control", @id = "i" })


Rendered HTML

Destination: <input class="form-control" id="d" name="StudentName" type="text" value="" />
Leaving From <input class="form-control" id="i" name="StudentName" type="text" value="" />


Should be returning with a length of two but is returning with a length of 0. above is the HTML and below is the JavaScript. I've tested it else where and works fine.

JavaScript

var a = document.getElementsByTagName("input");

for (i = 0; i < a.length; i++) {
a[i].addEventListener("focus", function () { showHint(a[i]) });

}

Answer Source

Try:

window.onload = function() {
      var a =  document.getElementsByTagName("input");

      for (i = 0; i < a.length; i++) {
          a[i].addEventListener("focus", function () { showHint(a[i]) });

      }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download