Praveen Singh Praveen Singh - 4 months ago 20
HTML Question

Find a word in page and replace with another word

I have a HTML page with lots of text in it and I want to find that text and replace it with some other text. I think it’s possible with jQuery. This is what I have:

HTML:

<div class="content"> <!-- this div is showing multiple times -->
ABC Drugs Finder helps you get the information and reviews on prescription drugs, over-the-counter medications, vitamins, and supplements. Search by salt name, brand name or medical condition.
</div>


I want to replace the word “Drugs” with something else, but somehow it’s not working. My jQuery code is:

$('#content').each(function() {
var text = $(this).text();
$(this).text(text.replace('Drugs', 'Drug'));
});

Answer

You have a defined class in HTML and an id in jQuery. Following is the correct jQuery code:

$('.content').each(function() {
   var text = $(this).text();
   $(this).text(text.replace('Drugs', 'Drug')); 
});