How do I wrap a span around the last word of an element's inner text, using jQuery? -


i know should simple task i'm having problems selecting heading elements last word , wrapping in span can add style changes.

here's have far

$('.side-c h3').split(/\s+/).pop().wrap('<span />'); 

any appreciated

the problem jquery objects wrap dom nodes. each word in element not dom node itself, you'll need little more work break apart text , rejoin it. need account multiple nodes being selected jquery. try this:

$('.side-c h3').each(function(index, element) {     var heading = $(element), word_array, last_word, first_part;      word_array = heading.html().split(/\s+/); // split on spaces     last_word = word_array.pop();             // pop last word     first_part = word_array.join(' ');        // rejoin first words      heading.html([first_part, ' <span>', last_word, '</span>'].join('')); }); 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -