-
-
Save faridv/409b871d29c04f198e20 to your computer and use it in GitHub Desktop.
Select the last word in an element & wrap it with a span tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($){ | |
$('h2.title').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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment