Color Repeats on All P tagged Elements

[Total: 0    Average: 0/5]

This code will find and color all letters in elements of type P.

Head Code:

<style>
.green   { color : green; }
.red     { color : red;   }
.yellow  { color : yellow;}
</style>
<script type="text/JavaScript">

var len=0;
var colors=new Array();
    colors[len++]="green";
    colors[len++]="red";
    colors[len++]="yellow";

function multi_color(element)
{
  var _str=element.innerHTML?element.innerHTML:"";
  var _strLen=_str.length?_str.length:0;
  var _color;
  var _result="";
  for(var i=0;i<_strLen;i++)
  {
    _color=colors[i%len];
    _result+='<span class="'+_color+'">'+_str[i]+'</span>'
  }
  element.innerHTML=_result;
}
function ex_init()
{
  var _A=document.getElementsByTagName("p");
  for(var i=0;i<_A.length?_A.length:0;i++)
  {
    var _obj=_A[i];
    multi_color(_obj);
  }
}
</script>

Init Code:

ex_init();

Body Code:

<p>dtrnir87rankpp79mp60mpy08p</p>
<p>guimmf8op9mpot97,7p7i889u</p>
<p>asl;kfja;sdfj;asdfj;adfsj</p>
<p>pqoiwuproqierpuqiowerupqoeru</p>

Times Viewed: 4

Leave a Reply

Your email address will not be published. Required fields are marked *