Individual elements in the document object model are the pieces that make up your webpage. Using JavaScript, after you query for them, you can manipulate them to your liking, and we'll go over the most-used manipulations.
Adding Classes
For many different scenarios, you'll want to be able to add a CSS class to an element. After you have queried for your element, like this:
HTML
<divclass="apple">Apples are tasty.</div>
JAVASCRIPT
const fruit = document.querySelector(".apple");
You can add a class to the element, like this:
JAVASCRIPT
const fruit = document.querySelector(".apple");
fruit.classList.add("tasty");
Now the element looks like this:
HTML
<divclass="apple tasty">Apples are tasty.</div>
Removing Classes
Removing a class is the same process in reverse. Once you have an element, you can remove a class from its class list, like so:
JAVASCRIPT
const fruit = document.querySelector(".apple");
fruit.classList.remove("tasty");
The class is no longer there.
Check if element contains class
Finally, to check if an element contains a class or not, simply call the contains function on the element's class list.
JAVASCRIPT
const fruit = document.querySelector(".apple");
fruit.classList.add("tasty");
if (fruit.classList.contains("tasty")) {
console.log("This fruit is tasty!");
} else {
console.log("This fruit is not tasty!");
}
HTML
This fruit is tasty!
Setting Properties
Properties on DOM elements can be manipulated in JavaScript because they are simply object properties. These are examples of some properties to work with:
value: The value of a tag (usually found on input tags).
href: The link of an anchor.
alt: The alternative text of an image.
title: Information about an element.
Let's say you want to dynamically set an anchor tag's href tag:
HTML
<a>Click here to go to Sabe.io</a>
JAVASCRIPT
const link = document.querySelector("a");
link.href = "https://sabe.io";
When the code runs, you'll get this:
HTML
<ahref="https://sabe.io">Click here to go to Sabe.io</a>
Getting Properties
Now, let's say you want to get the value of a input text field for a username.
Similar to properties, you can also manipulate an element's attributes. The only difference is that instead of accessing it as a property, this time you are using methods.
To get an attribute, simply use getAttribute and then pass in the name of the attribute you want.
Since we are asking for the src attribute, that is what we get back:
HTML
https://sabe.io/cat.png
Setting Attributes
Setting an attribute is just as easy as getting them. Use the setAttribute method which takes two parameters, the name of the attribute, and then its value.
HTML
<imgsrc="https://sabe.io/cat.png">
JAVASCRIPT
const animal = document.querySelector("img");
animal.setAttribute("alt", "This is a picture of a cat.");
Running this code gives you this element:
HTML
<imgsrc="https://sabe.io/cat.png"alt="This is a picture of a cat.">
Removing Attributes
Finally, to remove an attribute from an element, use removeAttribute and pass in the name of the attribute you want gone.