|
Page 1 of 1 |
|
Posted: Sat, 21st Jun 2014 02:57 Post subject: Select last word, apply class |
|
 |
Hello
I need to select the last word in each heading (h1, h2, h3 etc) and apply a class to it. Here is a heading for example:
Code: | <h1><a href="#">Car Detailing</a></h1> |
And the code I'm currently using (which somewhat works):
Code: | $('h1, h2, h3, h4').html(function(){
var text= $(this).text().split(' ');
var last = text.pop();
return text.join(" ") + (text.length > 0 ? ' <span class="heavy">'+last+'</span>' : last);
}); |
That works fine for headings, but it strips out the link in any heading that has one and I can't tell why.
Page:
https://dl.dropboxusercontent.com/u/34687130/inch_wip/index.html (not complete obviously)
Thanks for any help!
|
|
Back to top |
|
 |
|
Posted: Sat, 21st Jun 2014 05:36 Post subject: |
|
 |
Code: |
jQuery.fn.extend({
findLast: function(sel) {
var $last = this.find(sel).last();
return $last.length > 0?$last:this;
},
wrapLastWord: function(wrapper){
this.each(function(){
var lastWord = $(this).text().split(' ').pop();
if (lastWord.length > 0) {
var $last = $(this).findLast(':contains("' + lastWord + '")');
$last.html($last.text().substring(0,$last.text().length - lastWord.length)).append($(wrapper).append(lastWord));
}
});
}
});
$('h1,h2,h3,h4').wrapLastWord('<i class="buttery">');
|
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group
|
|
 |
|