The Web Dev Guide by Wishtack
  • The Web Dev Guide by Wishtack
  • HTML
    • HTML Tags
    • HTML Attributes
    • Content Formatting
    • Empty Tags vs Content Tags
    • Some Links
  • ECMAScript
    • Some History
    • Language Properties
    • Single-Threaded thus Asynchronous
    • The Event Loop
    • Classes
    • Hoisting is Dead: var vs. let vs. const
    • this & "binding"
    • Arrow Functions
    • Template Strings
    • Syntactic Sugar
      • Spread
      • Destructuring
      • Rest
      • Object Literal Property Value Shorthand
    • Named Parameters
    • Compatibility
  • Tools
    • Node.js
    • NPM
    • Yarn
    • Webpack
    • WebStorm
    • StackBlitz
  • DOM
    • What Is It?
    • Element Selection
    • Element Modification
    • Events
  • Forms
    • The <form> tag
    • Form elements
    • Form validation
  • Networking
    • Fetch Web API
  • CSS
    • Selectors
    • Transforms
    • Transitions
    • Animations
    • Web Animations API
    • Sass
  • Responsive Web Design
    • Viewport
    • Media Queries
    • Grid Layout
    • Flex Layout
    • Frameworks & Libraries
  • Web APIs
  • Testing
    • Unit Testing
    • End to End Testing
  • Security
    • Injection
    • DOM XSS
    • Insecure Direct Object Reference
    • Cross-Site Request Forgery
    • Client vs API Validation
    • API Unauthorized Access and Data Leak
  • More Links
Powered by GitBook
On this page
  • Modify content
  • Modify properties
  • Modify the structure
  • CSSOM (CSS Object Model)
  1. DOM

Element Modification

Once you retrieve the reference to an element in the DOM, you can modify any of the element's properties.

Modify content

element.textContent = 'Hello 🌏';

HTML content can be controlled using element.innerHTML attribute but this approach is dangerous as it can lead to XSS (Cross-Site Scripting) vulnerabilities.

In addition to the security issues, updating the innerHTML forces the browser to parse the HTML then update the DOM content. This can have bad performance results.

Modify properties

/* Toggle `disabled` state. */
button.disabled = !button.disabled;

link.src = `https://wishtack.io`;

Modify the structure

Create a DOM element

const child = document.createElement('div');

Append an element

parent.appendChild(child);

Remove an element

parent.removeChild(child);

CSSOM (CSS Object Model)

/* Update some CSS style. */
element.style.backgroundColor = 'red';

/* Added a CSS class to an element. */
element.classList.add('blog-post-content');

/* Toggle a CSS class. */
element.classList.toggle('important');

PreviousElement SelectionNextEvents

Last updated 6 years ago