Modifying elements
Here's an overview of the most important properties and methods for modifying elements in the DOM:
Properties for Modifying Elements
element.innerHTML- Sets or gets the HTML content within an element.element.innerText- Sets or gets the text content within an element, ignoring any HTML tags.element.value- Sets or gets the value of a form element, such as an input or textarea.element.src- Sets or gets the source URL of an image or other media element.element.href- Sets or gets the destination URL of a link element.element.style- Provides access to the inline style properties of an element.element.className- Sets or gets the class attribute of an element.
Methods for Modifying Elements
document.createElement(tagName)- Creates a new element with the specified tag name.element.appendChild(newNode)- Adds a new child node to the end of an element.element.insertBefore(newNode, referenceNode)- Inserts a new child node before a specified reference node within an element.element.removeChild(childNode)- Removes a child node from an element.element.replaceChild(newNode, oldNode)- Replaces a child node with a new node within an element.element.setAttribute(attributeName, attributeValue)- Sets or updates the value of a specified attribute on an element.element.removeAttribute(attributeName)- Removes a specified attribute from an element.
Examples
Let's see some examples of how these properties and methods can be used to modify elements in the DOM:
Modifying HTML content with innerHTML
Modifying text content with innerText
Modifying a form element value with value
Modifying an image source with src
Modifying a link destination with href
Modifying inline styles with style
Modifying classes with className
element.className: returns a string containing the value of the class attribute of an elementelement.className = "class-name": sets the class attribute of an element to a new valueExample:
Modifying classes with classList
element.classList: returns a list of the classes of an elementelement.classList.add("class-name"): adds a class to an elementelement.classList.remove("class-name"): removes a class from an elementelement.classList.toggle("class-name"): toggles a class on and off for an elementelement.classList.contains("class-name"): checks if an element has a particular classExample:
Modifying styles with style
element.style.property = "value": sets a style property for an elementExample:
modifying elements using methods
setAttribute(): This method is used to set the value of a specified attribute of an element. The syntax is:For example, to set the
srcattribute of an image element, you could use:appendChild(): This method is used to add a new child node to an element. The syntax is:For example, to add a new paragraph element to a
divelement, you could use:removeChild(): This method is used to remove a child node from an element. The syntax is:For example, to remove a paragraph element from a
divelement, you could use:replaceChild(): This method is used to replace a child node with a new node. The syntax is:For example, to replace a paragraph element with a new
h1element, you could use:More methods for modify attributes
element.getAttribute("attribute-name"): returns the value of an attribute for an elementelement.setAttribute("attribute-name", "value"): sets the value of an attribute for an elementelement.hasAttribute("attribute-name"): checks if an element has a particular attributeelement.removeAttribute("attribute-name"): removes an attribute from an elementExample:
Last updated