DOM stands for Document Object Model. It is a standard to access and share document or website over the Internet. It represents how a particular document is structured and it also helps to modify the website.
-
By ID
The
getElementById()
method returns an element with a specified value.
document.getElementById("heading");
-
By Tag Name
The
getElementByTagName()
method returns a collection of all elements with a specified tag name.
documnet.getElementsByTagName("h1");
-
By Class Name
The
getElementByClassName()
method returns a collection of elements with a specified class names.
document.getElementByClassName("text");
-
By CSS Selectors
The
querySelector()
method returns the first element that matches a CSS selector.The
querySelectorAll()
method returns all matches.
document.querySelectorAll("h2.text");
It represents an HTML element like
P
,DIV
,A
,TABLE
, or any other HTML element.
-
classList
Returns the class names of an element.
-
id
Returns the value of the id attribute of an element.
-
innerHTML
Returns the content of an element.
-
addEventListener()
Attaches an event handler to an element.
-
getBoundingClientRect()
Returns the size of an elements and its position relative to the viewport.
-
hasAttribute()
Returns true, if an element has a given attribute.
-
removeAttribute()
Removes an element from the DOM.
-
createElement()
The
createElement()
method creates an element node. -
appendChild()
The
appendChild()
method appends a node (element) as the last child of an element.
-
firstElementChild
The
firstElementChild
property returns the first child element of the specified element. -
lastElementChild
The
lastElementChild
property returns the last child element of an element. -
previousElementSibling
The
previousElementSibling
property returns the previous element in the same tree levell. -
nextElementSibling
The
nextElementSibling
property returns the next element in the same tree level.
-
remove()
The
remove()
method removes an element (or node) from the document.