⊗jsPmStCA 383 of 504 menu

Advantage of styling with CSS classes in JavaScript

Using classes instead of changing styles directly has another benefit. With a slight movement of the hand, you can make the styles of the elements toggle.

For example, you can make it so that when you first click on a paragraph, it will be painted in a certain color, and when you click again, it will return to its original color. To do this, you just need to change the add method to the toggle method:

let elems = document.querySelectorAll('p'); for (let elem of elems) { elem.addEventListener('click', function() { this.classList.toggle('colored'); }); }

Modify the previous task so that pressing the button again cancels the action of this button.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline