Praveen Singh Praveen Singh - 1 year ago 79
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:


<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.

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 Source

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')); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download