highlight text

HTML

<p><span id="container">I need to be highlighted one character at a time</span>
<input id="click" type="button" value="click me"/></p>
<img src="http://dl.dropbox.com/u/59918876/cursor.png" width="16"/>

CSS

#container{
 font-size: 16px;  
 margin-right: 10px;   
}
.highlight{
    background: yellow;           
}
img{
 position: relative;
 top: -10px;    
}

JavaScript

function highlight(){
    var text = $('#container').text(); //get text of container
    $('#click').css('border','none'); //remove the border
    $('img').css('left', '0px'); //reset the cursor left
    $('img').animate({left: $('#container').width() + 40}, text.length*70); //animation of cursor 
    $('#container').html('<span class="highlight">'+text.substring(0,1)+'</span><span>'+text.substring(1)+'</span>'); //set the first html
    (function myLoop (i) {//animation loop          
       setTimeout(function () {        
         var highlight = $('.highlight').text(); 
         var highlightAdd = $('.highlight').next().text().substring(0,1);;
         var plain = $('.highlight').next().text().substring(1);
         $('#container').html('<span class="highlight">'+highlight+highlightAdd+'</span><span>'+plain+'</span>');     
         if (--i) myLoop(i);//  decrement i and call myLoop again if i > 0
        }, 270)
     })(text.length);
     setTimeout(function () {   
        $('#click').css('border','1px solid black'); 
     }, text.length*85);
}
 
highlight();
var intervalID = setInterval(highlight, $('#container').text().length*110); 
//clearInterval(intervalID);