jquery - How to select a random letter from a link in a <li> and color it on hover? -


i'm building wordpress theme client , designer thought fun color 1 letter of menu items on hover. tried working lettering.js(http://letteringjs.com/), way i'd have specify letter want color in css , css became mess of nth-child selectors , whatnot.

i'd use jquery in way selects random letter (just 1 random letter, not multiple) link within list item , colours specified, on hover. i've found this: change color of random letters in paragraph , tried edit work case, couldn't work.

can come solution?

you can use string character array; can access index "abc"[0] or slice it, instance.

so given random index, index might able divide into

text.slice(0, index) // part before target character + text[index] // target character + text.slice(index+1) // part after target character 

a complete solution, then, might this:

    var text = $.trim($(this).text());     var index = getrandom(text.length);      $(this).html(         [text.slice(0, index), '<span class="colored">', text[index], '</span>', text.slice(index+1)].join('')     ); 

demo. reload re-randomize.

note doesn't cover things such randomized character being whitespace. simple solution might throw in like

while(text[index] == ' ') index = getrandom(text.length); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -