Why not added the styles to the JavaScript?

var str = 'text, Text, text, text, text, text';
var p = document.querySelector('p');
var str_arr = str.split(','); //split an array into elements as follows: [ 'Text', 'text', 'text', 'text', 'text', 'text' ]
var i = 0;
var colors = ['red', 'green', 'black', 'yellow', 'green', 'orange', 'gray']; //array of colors
p.innerHTML = str_arr[0]; 
setInterval(function(){ //create a time interval with a function that will change the color of the element of the array
 if(i == 7){ //number of colors in an array of colors
 i = 0;
}
 str_arr[0].style.color = colors[i]; //to replace this string to make it work?
i++;
}, 1000);
;

Fail styles for strings entered in a HTML tag...
March 23rd 20 at 19:16
2 answers
March 23rd 20 at 19:18
Solution
Well, I guess you have styles to element ask and not strings: p.style.color = colors[i].
and the element of the array in any way?
just the point is to change the color of each word in the line, and to put all the span and add the class as long... - Darian.Heidenreich69 commented on March 23rd 20 at 19:21
@Darian.Heidenreich69,

and the element of the array in any way?

Yes, you are crazy.

change the color of each word in the string <...> to put all span <...> long

Not long. - keely.Boye commented on March 23rd 20 at 19:24
Thanks for the answer) - Darian.Heidenreich69 commented on March 23rd 20 at 19:27
@Darian.Heidenreich69, no thanks, and celebrate the decision, it will be thanks. - garret_Beer commented on March 23rd 20 at 19:30
March 23rd 20 at 19:20
Solution
var str = 'text, Text, text, text, text, text';
var p = document.querySelector('p');
var str_arr = str.split(',');
var i = 0;
var colors = ['red', 'green', 'black', 'yellow', 'green', 'orange', 'gray']; //array of colors
p.innerHTML = str_arr[0]; 
setInterval(function(){ 
 p.style.color = colors[i];
 i = (i+1)%colors.length;
}, 1000);
Thank you! - Darian.Heidenreich69 commented on March 23rd 20 at 19:23
i = (i+1)%colors.length;
Very readable and most importantly fast-acting each time considered. - mohammed.Feeney75 commented on March 23rd 20 at 19:26
@mohammed.Feeney75, js and performance? no, not heard - quinten.Runolfss commented on March 23rd 20 at 19:29

Find more questions by tags HTMLJavaScript