Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `HTMLElement` and `Element`?

Tags:

javascript

What's the difference between HTMLElement and Element ?

document.createElement("div") instanceof Element gives true

document.createElement("div") instanceof HTMLElement gives true

HTMLElement === Element gives false

like image 502
Li Juan Avatar asked Jul 05 '11 11:07

Li Juan


People also ask

How do I know what type of HTML element I have?

If you just need to check for the specific type of element, you can use the .is() method, which returns a Boolean value: JS. HTML.

Are HTML tags and elements the same thing?

However, in common usage the terms HTML element and HTML tag are interchangeable i.e. a tag is an element is a tag. For simplicity's sake of this website, the terms "tag" and "element" are used to mean the same thing — as it will define something on your web page.

What is HTML element in angular?

Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.


2 Answers

An HTMLElement is an Element, as defined in the HTML extension to the DOM Level 2 Core specification:

interface HTMLElement : Element {            attribute DOMString       id;            attribute DOMString       title;            attribute DOMString       lang;            attribute DOMString       dir;            attribute DOMString       className; }; 

Element (per specification) refers to the Element interface as it is defined in the DOM Core Level 2 specification (there is also another DOM Core specification (working draft) though).

Update: There are a lot of specifications out there and it is not totally clear which browsers use which one (to me).

Maybe someone else has more insight...

But in any case, Element is a more generic interface than HTMLElement and the latter inherits from the former.

Update 2: A nice way to see the inheritance structure is to execute console.dir(elementReference) for any element (works in Chrome/Safari, needs Firebug for Firefox).

like image 197
Felix Kling Avatar answered Oct 08 '22 15:10

Felix Kling


HTMLElement refers explicitly to an HTML element whereas Element may refer to an XML element. However, HTMLElement is technically a subset of Element.

like image 32
jerluc Avatar answered Oct 08 '22 14:10

jerluc