Wrap element around a char

by btevfik

HTML

<div id="foo">Some random string</div>

CSS

span{
    color:red;
}

JavaScript

//get the current string
var string = $("#foo").html();

//find a random number to replace the char
var randomNum = Math.ceil(Math.random()*$("#foo").html().length-1);

//create the new character wrapped with span
var newChar = "<span>"+string[randomNum]+"</span>";

//create the new string
var newString=string.substring(0, randomNum)+newChar+string.substring(randomNum+1,string.length);

//change the inner html
$("#foo").html(newString);