Changing the page

Knowledge Videos

Short animated explainers, organised by subject.

Changing the page

Toggle a class with classList so CSS restyles an element, and build new elements with createElement and append, like a to-do list adding tasks.

Transcript

JavaScript can change more than text. Every element has a class list, and you can add, remove or toggle a class name. Your C S S then styles the element.

Here, toggle a class called selected on a card. The C S S rule for dot selected applies at once, and the card gets a gold border. Click again, and the class goes away.

You can also build new elements. Create element makes one in memory. Set its text content, then append it to a parent, and it appears on the page.

Put it together: when the button is clicked, create a list item, fill it with the text, and append it to the list. This is how a to-do list adds tasks.

Remember: class list changes the look through C S S. Create element and append add new things to the page.

More in this series