We can use getElementsByClassName to get all the elements with a class name ‘xyz’. We can also use document.querySelector and document.querySelectorAll to get elements with a class name ‘xyz’.
We can use getElementsByClassName(“xyz”) ot get the first element with a class name ‘xyz’.
Here is how to do it:
One more way to do is using the querySelector() method. When we use querySelector() method on multiple elements, it will only return the first element that matches the query.
Here’s how to do it:
There is another way to do it.
We can add a parent element for all those elements with class name ‘xyz’. Then we’ll add id attribute for the parent element whose child element we want to target it and then use the id and class both to target the element.
It’s just a long way but for the sake of the example, let’s check it out.