Learn about DOM (Document Object Model) in JavaScript and how to work with them.
|
getElementById() Find an Element by ID |
The getElementById() function returns the Element object whose HTML id attribute value matches the specified string. If no matching element exists, it returns null. |
|---|---|
|
getElementsByTagName() Find All Elements by Tag Name |
The getElementsByTagName() function returns an HTMLCollection object containing all elements that match the specified tag name. If no matching elements are found, it returns an empty HTMLCollection object. |
|
getElementsByClassName() Find All Elements by Class Name |
The getElementsByClassName() function returns an HTMLCollection object containing all elements that match the specified class name. If no matching elements are found, it returns an empty HTMLCollection object. |
|
HTMLCollection An Object Containing Selected HTML Elements |
The HTMLCollection is a collection of elements selected from an HTML document, arranged in the order they appear in the DOM. |
|
NodeList An Object Containing Selected Nodes |
The NodeList is a collection of nodes selected from a web page, arranged in the order they appear in the DOM. |
|
HTMLCollection vs. NodeList Understanding the Concepts and Differences |
When working with the DOM in JavaScript, multiple selected HTML elements are often returned as collections. These DOM collections include HTMLCollection and NodeList. |
|
querySelector() Select the First Element Using a CSS Selector |
The querySelector() function returns the first element that matches the specified CSS selector within the HTML document or a specific element. Returns null if no match is found. |
|
querySelectorAll() Select All Elements Using a CSS Selector |
The querySelectorAll() function returns a static NodeList containing all elements that match the specified CSS selector within the HTML document or a specific element. |
|
querySelector() vs. querySelectorAll() Key Differences Explained |
The querySelector() function returns the first matching element, while the querySelectorAll() function returns all elements that match the specified CSS selector. |
|
closest() Finding the Closest Ancestor Element |
The closest() function finds and returns the closest ancestor element that matches the given CSS selector. The element itself is also included in the search. |
|
getAttribute() Getting Attribute Values from Elements |
The getAttribute() function retrieves the value of a specified attribute from a DOM element. If the specified attribute exists on the element, it returns its value as a string. |
|
setAttribute() Setting or Adding Attributes to Elements |
The setAttribute() function sets an attribute on a DOM element. If the attribute already exists, its value is updated; otherwise, a new attribute is added with the specified name and value. |
|
removeAttribute() Removing Specified Attributes from Elements |
The removeAttribute() function removes the attribute specified as a parameter from a DOM element. If the specified attribute does not exist on the element, it does nothing without throwing an error. |
|
hasAttribute() Checking If an Element Has an Attribute |
The hasAttribute() function checks whether a DOM element has a specified attribute. It returns true if the element has the attribute, and false if it does not. |
|
toggleAttribute() Toggling a Boolean Attribute on an Element |
The toggleAttribute() function adds a specified Boolean attribute to a DOM element if it does not exist, and removes it if it does. It returns true if the attribute is added, and false if it is removed. |
|
Manipulating the Class Attribute classList Object |
The classList is an object that allows you to easily manipulate the class attribute of DOM elements. It provides several useful methods and properties for adding, removing, replacing, and checking class values. |
|
classList.add() Add Class Values to an Element |
The classList.add() method adds the specified class value to a DOM element. You can add a single class value or multiple class values at once. |
|
classList.remove() Remove Class Values from an Element |
The classList.remove() method removes the specified class value from a DOM element. You can remove a single class value or multiple class values at once. |
|
classList.toggle() Toggle Class Values on an Element |
The classList.toggle() method adds the specified class attribute value to a DOM element if it does not exist, and removes it if it does. It returns true if the class value is added, and false if it is removed. |
|
classList.contains() Check Class Values of an Element |
The classList.contains() method checks whether the specified class attribute value exists in a DOM element. It returns true if the value exists and false otherwise. |