56
<div id="id1">
 <p>
   apple
 </p>
 <p>
   ball
 </p>
 <p>
   cat
 </p>
 <p>
   dogsss
 </p>
</div>

How Do I change dogsss to dollsss using jquery?

william
  • 7,284
  • 19
  • 66
  • 106
  • No, Tkz for all, But you have misunderstood my meaning.. `sss` is a text that I wouldn't know what will it b exactly.. But I want to change `dog` to `doll` – william Nov 16 '11 at 04:57
  • Similar to C# replace `string temp="tasting";temp=temp.Replace("tast","test");` sth like that. – william Nov 16 '11 at 05:00

10 Answers10

119

You can use .each() to loop through the <p> elements, and .text() to update the text.

For example:

$('#id1 p').each(function() {
    // get element text
    var text = $(this).text();
    // modify text
    text = text.replace('dog', 'doll');
    // update element text
    $(this).text(text); 
});

Demo: https://jsfiddle.net/8gra4xjw/


[Updated to reflect comments]

Note: The above replaces the first occurrence of 'dog' only. To replace all occurrences, you could use:

// modify text (replace all)
text = text.replace(/dog/g, 'doll');

See also: How to replace all occurrences of a string in JavaScript


If the new text must contain HTML entities like &nbsp;, you could use:

// update element text (html)
$(this).html(text);

See also: What is the difference between jQuery: text() and html() ?

Doug Owings
  • 4,438
  • 1
  • 21
  • 21
33
$("#id1 p:contains('dog')").html("doll");

that'll do it.

http://jsfiddle.net/pgDFQ/

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • 3
    what if `p` contains **dog is barking**? – Vikrant Sep 11 '18 at 06:51
  • @Vikrant You are absolutely correct. This solution I had provided is incomplete and it was written at an early point in my development career. I've considered removing it in the past, but I've kept it around for the fact that it includes the narrowing selector `contains()` and has enough reputation to draw attention to it. I like the accepted answer. I upvoted it, but the only reason this answer isn't deleted is because I think it still adds value to this question. I'm, however, fully willing to entertain other opinions on if it should be removed or not. – Joseph Marikle Sep 11 '18 at 11:56
  • @JosephMarikle you could edit the answer and add more value to it to cover other cases. :) – boroboris Mar 15 '21 at 15:55
  • `let p = $("#id1 p:contains('dog')"); p.text(p.text().replace(/\bdog\b/g, 'doll'));` – Nils Lindemann Apr 30 '21 at 09:45
13

try this,

$('#id1').html($('#id1').html().replace('dogsss','dollsss'));

working sample

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
9

Warning string.replace('string','new string') not replaced all character. You have to use regax replace.

For exp: I have a sting old string1, old string2, old string3 and want to replace old to new

Then i used.

    var test = 'old string1, old string2, old string3';

   //***** Wrong method **********
    test = test.replace('old', 'new'); // This  will replaced only first match not all
    Result: new string1, old string2, old string3

  //***** Right method **********
    test = test.replace(/([old])+/g, 'new'); // This  will replaced all match 
    Result: new string1, new string2, new string3
Sandeep Sherpur
  • 2,418
  • 25
  • 27
8
$('p:contains("dogsss")').text('dollsss');
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
5

More specific:

$("#id1 p:contains('dog')").text($("#id1 p:contains('dog')").text().replace('dog', 'doll'));
Doc Kodam
  • 723
  • 6
  • 14
2

I was looking for something similar and I use code that Doug Owings posted, but my text had some br tags and the code was erasing it.

So I use this: ( Just note that I replaced .text() to .html() )

Text:

< p class = "textcontent" > 
     Here some text replace me 
     < br > here an other text
     < br > here is more text 
< /p>

JS:

$('.textcontent').each(function() {

   var text = $(this).html();
   $(this).html(text.replace('replace me', 'I love this text')); 

});

Also if you want to edit several text create an array:

var replacetext = {
    "Text 0": "New Text 0",
    "Text 1": "New Text 1",
    "Text 2": "New Text 2",
    "Text 3": "New Text 3",
    "Text 4": "New Text 4"
};

$.each(replacetext, function(txtorig, txtnew) {
    var text = $('#parentid').html();
    $('#parentid').html(text.replace(txtorig, txtnew)); 
});
2

How to change multiple "dogsss" to "dollsss":

$('#id1 p').each(function() {
var text = $(this).text();
// Use global flag i.e. g in the regex to replace all occurrences
$(this).text(text.replace(/dog/g, 'doll'));
});

https://jsfiddle.net/ashjpb9w/

Chris
  • 21
  • 1
0

Change by id or class for each tag

$("#id <tag>:contains('Text want to replaced')").html("Text Want to replaced with");


$(".className <tag>:contains('Text want to replaced')").html("Text Want to replaced with");
0

Try This

$("#id1 p:contains('dogsss')").replaceWith("dollsss");
Ali Chraghi
  • 613
  • 6
  • 16