Advanced JavaScript Training
Attribute Manipulation


Attribute Manipulation

The DOM provides methods for getting and setting attributes of elements. getAttribute and setAttribute allow you to read and modify attributes dynamically.

  
    // Select the first element with class 'highlight'
    let link = document.querySelector('.highlight'); 
    // Get the 'href' attribute of a link 
    let linkHref = link.getAttribute('href'); 
    // Get a reference to the element with the id 'pageTitle' 
    let image = document.getElementById(“image'); 
    // Set the 'alt' attribute of an image  
    image.setAttribute('alt', 'New Alt Text');  
  
Have a doubt?
Post it here, our mentors will help you out.