jQuery - Wrap each word of an element in a unique span tag

      // Wrap each word in a span.
      $('#main-nav a').each(function(){
           var text = $(this).text().split(' ');
 
           for( var i = 0, len = text.length; i < len; i++ ) {
               text[i] = '<span class="word-' + i + '">' + text[i] + '</span>';
           }
           $(this).html(text.join(' '));
 
      });

Tags:

Comments

Thanks for your help on the span tag. Really appreciate it!

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.