Are you sure you're getting this? Fill in the missing part by typing it in.
Selecting elements from the DOM is one of the basic operations in frontend development. JavaScript provides several methods to select elements based on their attributes, tag name, or CSS selectors. One commonly used method is querySelector
, which allows you to select an element using a CSS selector. For example, to select all <p>
elements with a class of highlight
, you can use the following code:
1document.querySelector('p.highlight');
Another method is getElementById
, which selects an element with a specific ID. For example, to select an element with the ID myElement
, you can use the following code:
1document.getElementById('myElement');
It's also possible to select elements by their class name using the getElementsByClassName
method. This method returns a collection of elements with the specified class name. For example, to select all elements with the class name myClass
, you can use the following code:
1document.getElementsByClassName('myClass');
By using these methods, you can easily select the desired elements from the DOM and manipulate them as needed.
Write the missing line below.