Wrap the last word of a paragraph in span tags using jQuery code example
Example 1: Wrap the last word of a paragraph in span tags using jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Phasellus sit amet auctor velit, ac egestas augue.</p>
Example 2: Wrap the last word of a paragraph in span tags using jQuery
jQuery(document).ready(function($){
$('p').html(function(){
var text= $(this).text().split(' ');
var last = text.pop();
return text.join(" ") + (text.length > 0 ? ' <span class="last">'+last+'</span>' : last);
});
});