0

I would like to find the element type of a tag and even tried looking in the spec here: http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#elements-in-the-dom

I thought I can do this document.createElement('button').__proto__ to get HTMLButtonElement, but not very clear that is what it is. Is there a way to find this out? Preferably something I can write out to the console or maybe there is a better reference that lists them all more clearly?

TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • possible duplicate of [Javascript isDOM -- How do you check if a Javascript Object is a DOM Object?](http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object) – Eric Jan 29 '14 at 13:13
  • 1
    NOT a duplicate. Question is how to find out the DOM type, not to check against other DOM types. I don't know the DOM types ahead of time and want to extract it, not compare it to another. – TruMan1 Jan 29 '14 at 13:44
  • 1
    `window.toString.call( element )`? The possible classes [are here](https://developer.mozilla.org/en-US/docs/Web/API) – megawac Jan 30 '14 at 04:22
  • @megawac, that actually works! Great find!! Pls post an answer and I will mark it correct. You can also do this: document.createElement('img').toString() – TruMan1 Feb 02 '14 at 02:32

2 Answers2

3

Calling toString on an element should return a string of the elements super class as I mentioned in comments (and is actually quite a common technique used in libraries eg toString.call([]) === "[object Array]") for array checking.

Anyway getting on with it, you can get the elements super class via toString or element.constructor call

var element = document.createElement("span");
var toString = window.toString;

toString.call(element) === "[object HTMLSpanElement]"; //true
element.constructor === HTMLSpanElement; //true
megawac
  • 10,953
  • 5
  • 40
  • 61
-1

Use the property 'nodeType'. If your element is 'x', then 'x.nodeType' will give you type of 'x'.

If you want the name of the node's tag, then use the 'nodeName' property.

halkujabra
  • 2,844
  • 3
  • 25
  • 35
  • 1
    Incorrect. The `nodeType` property returns a numeric code representing the type of node (element, text, comment, etc). See [Node.nodeType](https://developer.mozilla.org/en-US/docs/Web/API/Node.nodeType) for more info. – Eric Jan 29 '14 at 14:35
  • The question was asked about type. Even regarding your comment, I have added to answer. – halkujabra Jan 30 '14 at 04:19