Manipulating attributes
We have seen already that we can change the class and style attributes, but there is a more general method that can be used to change any attribute. Just a quick reminder, attributes are the parts in HTML elements that are followed by equals signs. For example, this HTML link to Google:
<a id="friend" class="fancy boxed" href="https://www.google.com">Ask my friend here.</a>
The attributes in this example are id
, class
, and href
. Other common attributes are src
and style
, but there are many others out there.
With the setAttribute()
method, we can add or change attributes on an element. This will change the HTML of the page. If you inspect the HTML in the browser you will see that the changed attributes are visible. You can do this from the console and see the result easily, or write another HTML file with this built in as a function. In this HTML snippet, you will see it in action:
<!DOCTYPE html...