HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-03-09
1 <p>We found the elements, and then what? Now we need to visually change them somehow. Fortunately, we found that the layout designer thoughtfully created styles for different states of the products. In style.css, you can find different CSS rules:</p>
1 <p>We found the elements, and then what? Now we need to visually change them somehow. Fortunately, we found that the layout designer thoughtfully created styles for different states of the products. In style.css, you can find different CSS rules:</p>
2 <ul><li>class product--available is most likely needed for products in stock;</li>
2 <ul><li>class product--available is most likely needed for products in stock;</li>
3 <li>and product--unavailable is added, apparently, when the products are out of stock;</li>
3 <li>and product--unavailable is added, apparently, when the products are out of stock;</li>
4 <li>and the product--special class is more than likely responsible for special offers.</li>
4 <li>and the product--special class is more than likely responsible for special offers.</li>
5 </ul><p>But why are we playing the guessing game? Let’s add classes and see what we get.</p>
5 </ul><p>But why are we playing the guessing game? Let’s add classes and see what we get.</p>
6 <p>Since DOM-elements are objects, they all have a set of properties and methods. One of the properties of DOM elements is the classList object. It contains methods for managing DOM element classes, including the add() method. We can use it to specify which class you want to add to the element.</p>
6 <p>Since DOM-elements are objects, they all have a set of properties and methods. One of the properties of DOM elements is the classList object. It contains methods for managing DOM element classes, including the add() method. We can use it to specify which class you want to add to the element.</p>
7 <p>The syntax is simple. First, we specify the DOM element to which we want to add a class, then access the classList property using dot and call the add() method by transferring to it a string with the required class. Note that you don’t need to have a dot before the class name. As a result, adding a class looks like this:</p>
7 <p>The syntax is simple. First, we specify the DOM element to which we want to add a class, then access the classList property using dot and call the add() method by transferring to it a string with the required class. Note that you don’t need to have a dot before the class name. As a result, adding a class looks like this:</p>
8 // When searching for an element by class, use dot var product = document.querySelector('.product'); // But when we add a class, there is no dot! product.classList.add('product--sale');<p>The result of classList.add() is the same as when we manually add a class to the layout:</p>
8 // When searching for an element by class, use dot var product = document.querySelector('.product'); // But when we add a class, there is no dot! product.classList.add('product--sale');<p>The result of classList.add() is the same as when we manually add a class to the layout:</p>
9 &lt;!-- Initial markup state --&gt; &lt;li class="product"&gt; … &lt;/li&gt; &lt;!-- State after calling classList.add --&gt; &lt;li class="product product--sale"&gt; … &lt;/li&gt;<p>Add to the found elements the class of the product that is not available and the special offer class.</p>
9 &lt;!-- Initial markup state --&gt; &lt;li class="product"&gt; … &lt;/li&gt; &lt;!-- State after calling classList.add --&gt; &lt;li class="product product--sale"&gt; … &lt;/li&gt;<p>Add to the found elements the class of the product that is not available and the special offer class.</p>