I'm trying to find a particular character in a div and wrap it in a span
tag.
I thought I could use something like:
$('.breadcrumb:contains("»")').replaceWith('<span>»</span>');
But this changes the whole breadcrumb div.
What am I doing wrong?
I'm trying to find a particular character in a div and wrap it in a span
tag.
I thought I could use something like:
$('.breadcrumb:contains("»")').replaceWith('<span>»</span>');
But this changes the whole breadcrumb div.
What am I doing wrong?
Share Improve this question edited Apr 20, 2019 at 19:09 double-beep 5,53719 gold badges40 silver badges49 bronze badges asked Dec 16, 2010 at 10:03 AdiAdi 4,03213 gold badges58 silver badges80 bronze badges2 Answers
Reset to default 3.replaceWith
only works on nodes. You need the string method .replace()
instead:
var $bc = $('.breadcrumb');
$bc.html($bc.text().replace('»', '<span>»</span>'));
Like Mr. Craver suggested, you can also call:
$bc.html(function(i, html) {
return html.replace('»', '<span>»</span>');
});
Example: http://www.jsfiddle/jwJKr/
var $bc = $('.breadcrumb');
$bc.html($bc.text().split('»').join('<span>»</span>'));
Works better to replace all characters.