Suppose I have the following HTML snippet:
<input type="text" id="myinput" />
Now I want to get that DOM element using JavaScript:
var element = document.getElementById("myinput");
Works fine, no problem so far.
But when I print it inside an alert box using alert(element);, it displays object HTMLInputElement.
Is there a way to get that element name (HTMLInputElement) as a string?
(Notice that when saying “element name” I do not mean the name attribute of an element, but the name how it is displayed when using alert() for example, as described above.
In some browsers, such as Firefox (and Chrome, potentially others) you can do:
But in general it’s a bit more complicated, perhaps not even totally reliable. The easiest way might be as such:
[Edit]
Or, using the “nodeName” attribute, you could write a utility function which should be generally much more reliable:
(Thanks @Esailija for the smarter implementation, @Alohci for pointing out exceptional cases.)