Modifying elements

Here's an overview of the most important properties and methods for modifying elements in the DOM:

Properties for Modifying Elements

  1. element.innerHTML - Sets or gets the HTML content within an element.

  2. element.innerText - Sets or gets the text content within an element, ignoring any HTML tags.

  3. element.value - Sets or gets the value of a form element, such as an input or textarea.

  4. element.src - Sets or gets the source URL of an image or other media element.

  5. element.href - Sets or gets the destination URL of a link element.

  6. element.style - Provides access to the inline style properties of an element.

  7. element.className - Sets or gets the class attribute of an element.

Methods for Modifying Elements

  1. document.createElement(tagName) - Creates a new element with the specified tag name.

  2. element.appendChild(newNode) - Adds a new child node to the end of an element.

  3. element.insertBefore(newNode, referenceNode) - Inserts a new child node before a specified reference node within an element.

  4. element.removeChild(childNode) - Removes a child node from an element.

  5. element.replaceChild(newNode, oldNode) - Replaces a child node with a new node within an element.

  6. element.setAttribute(attributeName, attributeValue) - Sets or updates the value of a specified attribute on an element.

  7. 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 element

  • element.className = "class-name": sets the class attribute of an element to a new value

  • Example:

Modifying classes with classList

  • element.classList: returns a list of the classes of an element

  • element.classList.add("class-name"): adds a class to an element

  • element.classList.remove("class-name"): removes a class from an element

  • element.classList.toggle("class-name"): toggles a class on and off for an element

  • element.classList.contains("class-name"): checks if an element has a particular class

  • Example:

Modifying styles with style

  • element.style.property = "value": sets a style property for an element

  • Example:

modifying elements using methods

  1. setAttribute(): This method is used to set the value of a specified attribute of an element. The syntax is:

    For example, to set the src attribute of an image element, you could use:

  2. 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 div element, you could use:

  3. 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 div element, you could use:

  4. 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 h1 element, you could use:

  5. More methods for modify attributes

  • element.getAttribute("attribute-name"): returns the value of an attribute for an element

  • element.setAttribute("attribute-name", "value"): sets the value of an attribute for an element

  • element.hasAttribute("attribute-name"): checks if an element has a particular attribute

  • element.removeAttribute("attribute-name"): removes an attribute from an element

  • Example:

Last updated