Search
 
SCRIPT & CODE EXAMPLE
 

HTML

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>
<script>
jQuery(document).ready(function($){  
		
	$('p').html(function(){	
		// separate the text by spaces
		var text= $(this).text().split(' ');
		// drop the last word and store it in a variable
		var last = text.pop();
		// join the text back and if it has more than 1 word add the span tag
		// to the last word
		return text.join(" ") + (text.length > 0 ? ' <span class="last">'+last+'</span>' : last);   
	});

});
</script>
<style>
span{
color:red;
}
</style>

Comment

PREVIOUS NEXT
Code Example
Html :: set icon website 
Html :: laravel blade remove all html tags 
Html :: html white space on both sides of the page 
Html :: add icon to website tab 
Html :: html change viewport to smartphone size 
Html :: input datetime 
Html :: meta redirect header 
Html :: twitter icon html5 
Html :: & in html 
Html :: make text vertical align middle in table bootstrap 
Html :: href disable underline 
Html :: iframe pdf html5 
Html :: open whatsapp html 
Html :: make a basic html page 
Html :: html insert pdf 
Html :: textarea readonly 
Html :: align image center of webpage 
Html :: dropdown menu default value 
Html :: email link html 
Html :: This request has been blocked; the content must be served over HTTPS. 
Html :: discernible name 
Html :: Html meta responsive viewport metatag 
Html :: html void 
Html :: how to make links in html 
Html :: ocr 
Html :: email template code 
Html :: how to display a link in html 
Html :: twig keys 
Html :: bs bg color 
Html :: html color text 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =