Share:
Notifications
Clear all

DOM Manipulation

1 Posts
1 Users
0 Reactions
706 Views
(@simran)
Posts: 18
Active Member
Topic starter
 

DOM manipulation refers to the process of dynamically changing the Document Object Model (DOM) structure, style, and content of a web page using JavaScript or libraries like jQuery. The DOM represents the structure of a webpage as a tree of objects, where each element (like paragraphs, images, and divs) is a node that can be manipulated.

Key Concepts of DOM Manipulation

  1. Selecting Elements:

    • To manipulate elements, you first need to select them. This can be done using various methods:
      • JavaScript:
        • document.getElementById('id'): Selects an element by its ID.
        • document.querySelector('.class'): Selects the first element matching the selector.
        • document.querySelectorAll('selector'): Selects all elements matching the selector.
      • jQuery:
        • $('#id'): Selects an element by ID.
        • $('.class'): Selects all elements with the specified class.
  2. Changing Content:

    • You can change the content of an element using properties like innerHTML, textContent, or methods such as .html() in jQuery:

    // JavaScript
    document.getElementById('myElement').innerHTML = 'New content';
    
    // jQuery
    $('#myElement').html('New content');
    
  3. Modifying Styles:

    • You can change the style of elements using the style property in JavaScript or .css() method in jQuery:
    // JavaScript
    document.getElementById('myElement').style.color = 'red';
    
    // jQuery
    $('#myElement').css('color', 'red');
    
  4. Adding and Removing Elements:

    • You can create new elements and insert them into the DOM, or remove existing ones:

    // JavaScript
    const newElement = document.createElement('div');
    newElement.textContent = 'Hello World!';
    document.body.appendChild(newElement); // Add to the end of the body
    
    // Remove an element
    const elementToRemove = document.getElementById('removeMe');
    elementToRemove.parentNode.removeChild(elementToRemove);
    
    // jQuery
    $('#myElement').remove(); // Remove an element
    $('body').append('<div>Hello World!</div>'); // Add a new element
    
  5. Event Handling:

    • You can attach event listeners to elements to respond to user interactions:

    // JavaScript
    document.getElementById('myButton').addEventListener('click', function() {
        alert('Button clicked!');
    });
    
    // jQuery
    $('#myButton').on('click', function() {
        alert('Button clicked!');
    });
    
  6. Traversing the DOM:

    • You can navigate through the DOM to find parent, child, and sibling elements:

    // JavaScript
    const parent = document.getElementById('myElement').parentNode;
    const children = document.getElementById('myElement').children;
    
    // jQuery
    $('#myElement').parent(); // Get parent
    $('#myElement').children(); // Get children
    

Advantages of DOM Manipulation

  • Dynamic Content: Allows for real-time updates to content without reloading the page.
  • Enhanced User Experience: Makes it possible to create interactive features like dropdowns, modals, and image sliders.
  • Efficient UI Updates: Only specific parts of the DOM can be updated, improving performance compared to full page reloads.
 
Posted : 02/11/2024 3:58 pm
Share: