Answer the question
In order to leave comments, you need to log in
How to pass variables to styles in JavaScript?
let lights = Array.from(document.querySelectorAll('.light'))
let speedValue = 250
for (let node of lights) {
node.style.animationDuration = `${speedValue}ms`
}
<div className='lights'>
<div className='light'></div>
<div className='light'></div>
<div className='light'></div>
<div className='light'></div>
<div className='light'></div>
<div className='light'></div>
<div className='light'></div>
</div>
node.style.animationDuration
I manually write a value, for example "250ms"
, Uncaught TypeError: lights is not iterable
Answer the question
In order to leave comments, you need to log in
querySelectorAll still returns an array, it does not need to be additionally wrapped with another array.
Without Array.from everything enumerates fine
node.style - a string, you need to parse it into an array and then assemble it back into a string or do a pattern replacement /([-\w]+)\s*:\s*([^;]+);?/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question