Lakshya Raj
Lakshya Raj

Reputation: 1775

What different JS classes can HTML Elements be?

What I want to know is what different JavaScript classes can HTML Elements be? For example:

  1. Lots of HTML elements are of class HTMLElement.

  2. <svg> is of class SVGElement.

Are there any other classes for elements that should be taken account of?

Upvotes: 0

Views: 85

Answers (1)

Scott Marcus
Scott Marcus

Reputation: 65808

I am building a JS library and I want to be able to make an HTML element given by the user into an object (that contains methods to perform). It should be able to take a single element, or an array of elements. All I want to test is if it is an element, then pass [x], else pass x because my object requires an array (of HTML elements). So I want to test for if x is an element like that (and I want to allow all types of elements too).

Then all you have to do is check the argument to see if its .nodeType === 1.

let el = document.querySelector("div");
let att = el.getAttribute("title");
console.log(el.nodeType, el.nodeName);
console.log(el.firstChild.nodeType, el.textContent);
<div title="I'm the value of an attribute node">I'm a text node within an element node.</div>

Upvotes: 3

Related Questions