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(' '));

  });

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.