<h1 id="block ">This is my header block</h1>
I want to change the block word’s color every 2 seconds in javascript
let colorss = ["blue", "green", "pink", "purple"]
let curColor = 0
changeColor = () =>{
let element = document.querySelector("#block")
element.style.color = colors[curColor]
curColor++
if (curColor >3) {
curColor = 0
}
setTimeout(changeColor, 2000)
}
setTimeout(changeColor, 2000)
I did this it works changes the color of h1 tag every 2 seconds.
I wanna apply apply it to "block" word how can i do?
3
Answers
You can wrap the word you want to select in another tag, e.g.
<span>
.I agree with the two solutions!
But if you want to set your color at start and don’t want a black block.
You can do this :
In addition, you may pass the colors array and the desired selector as an argument in your function.