← CodePrimer
JavaScript Beginner Dom

setAttribute and style

How do I change attributes and CSS from JS?

Attributes + inline styles.

Sample
const el = document.querySelector('#banner');
el.setAttribute('class', 'banner is-active');
el.style.color = 'blue';

Notes

For classes, classList.add/remove/toggle is usually cleaner.

Keywords: setAttribute · style · class · dom

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share