DOM Manipulation in JavaScript

DOM Manipulation in JavaScript

DOM (Document Object Model) manipulation in JavaScript is a crucial aspect of web development, allowing you to interact with and manipulate the structure and content of HTML and XML documents dynamically. Here's a basic overview of DOM manipulation in JavaScript:

Accessing DOM Elements:

  • getElementById:

let element = document.getElementById('elementId');        

  • getElementsByClassName:

let elements = document.getElementsByClassName('className');        

  • getElementsByTagName:

let elements = document.getElementsByTagName('tagName');        

  • querySelector:

let element = document.querySelector('selector');        

  • querySelectorAll:

let elements = document.querySelectorAll('selector');        

Manipulating Element Content:

  • innerHTML:

element.innerHTML = 'New content';        

  • innerText:

element.innerText = 'New text';        

  • textContent:

element.textContent = 'New text content';        

  • Modifying Attributes:

element.setAttribute('attributeName', 'attributeValue');        

  • Changing Styles:

element.style.property = 'value';        

  • Creating New Elements:

let newElement = document.createElement('tagName');        

  • Appending and Removing Elements:

parentElement.appendChild(newElement);  // Append
parentElement.removeChild(childElement);  // Remove        

  • Event Handling:

element.addEventListener('event', function() {
    // Your event handling code
});        

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>DOM Manipulation</title>
  <style>
    .highlight {
      color: blue;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <h1 id="mainHeading">Hello, DOM!</h1>
  <button id="changeTextButton">Change Text</button>
  <script>
    document.addEventListener('DOMContentLoaded', function() {
      let heading = document.getElementById('mainHeading');
      let button = document.getElementById('changeTextButton');

      button.addEventListener('click', function() {
        heading.innerText = 'Text Changed!!!!';
        heading.classList.add('highlight');
      });
    });
  </script>
</body>
</html>
        

In this example, clicking the button changes the text of the heading and adds a CSS class to highlight it. The script is placed at the end of the body to ensure the DOM elements are available when the script runs.

要查看或添加评论,请登录

Shashank Kanojia的更多文章

  • DSA Day 1

    DSA Day 1

    Difference Between Array And List in Python In Python, the terms "array" and "list" refer to two different data…

  • Vite vs Create React App

    Vite vs Create React App

    Vite and Create React App (CRA) are both build tools that help developers set up and manage modern web development…

  • Microservices Architecture with Real World Example

    Microservices Architecture with Real World Example

    Microservices architecture is an architectural style that structures an application as a collection of small…

社区洞察

其他会员也浏览了