Attributes within certain elements can be changed as well.
Viewing attributes
Firstly, to view the attributes present in an element, we use
document.querySelector("a").attributes;
This returns an array-like object that holds individual attribute objects within the element. We do not modify using attributes
this is just to see what’s there.
Getting attributes
We use a getter method for this -
document. querySelector("a").getAttribute("href");
// returns "https://www.website.com"
Changing/Setting attributes
We use setAttribute
method that takes two arguments -
- attribute to be changed
- new value
document. querySelector("a").setAttribute("href", "www.bing.com");