First of all, the CSS method will only work on jquery objects. You have strings in an array of characters. The css method will not work with strings.
Secondly, each of your methods is spelled incorrectly. It should be like
$.each(character, function(index, value) {
For your operator, the problem is to change the color of some random characters in your string. Here is the fiddle . Try it.
Here is the code:
$('input').click(function(){ var para = $('#para'); var charArray = $('span', para); // case: No span (Initial String) if (charArray.length === 0) { var html = para.html(); var newArr = []; var len = html.length; for (var i=0; i<len; i++) { newArr.push('<span>' + html[i] + '</span>'); } html = newArr.join(''); para.html(html); charArray = $('span', para); } // Reset all spans $.each(charArray, function(i, value) { value.style.color = ''; }); var paralen = charArray.length; for (var i=0; i<30; i++) { var pos = Math.floor(Math.random()*100); pos = pos % paralen; charArray[pos].style.color = 'red'; } });
source share