Reputation: 859
I am trying to use jQuery to search for all instances of the word "contracting" and wrap it in
<strong>
tag. Is there any way to do this using jQuery? The end result I'd be looking for is
<strong>contracting</strong>.
Upvotes: 2
Views: 6004
Reputation: 1
(function(){
$("p").each(function() {
var self = $(this);
self.html(self.html().replace(/contracting/gi, "<strong>$&</strong>"));
})
})()
;
Upvotes: 0
Reputation: 707
You will have to have your content you are replacing surrounded by something <span>
<div>
with an ID for this to work.
HTML:
<div id="content">Contracting</div>
JS:
content=document.getElementById("content");
content.innerHTML=content.innerHTML.replace(/contracting/gi,"<strong>$&</strong>");
JSfiddle:http://jsfiddle.net/jwTz8/
The JSfiddle works but the font doesn't change for me, you may have to inspect the element (right click Contracting, inspect element) to see the strong tags are there.
Upvotes: 1
Reputation: 8700
How about this?
var yourElement = $("#your-element-selector");
yourElement.html(yourElement.html().replace("yourword", "<strong>yourword</strong>"));
Upvotes: 0
Reputation: 47776
You could do something like this:
$("p").each(function() {
$(this).html($(this).html().replace(/contracting/g, "<strong>contracting</strong>"));
});
Upvotes: 6