Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: Find Text and replace

Tags:

jquery

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

like image 762
william Avatar asked Nov 16 '11 04:11

william


People also ask

How to remove specific text in jQuery?

split("By:"). join("")); }); This removes any occurrence of "By:" in any element. If you want to target specfic type of elements, simply change $(':contains("By:")') to include whatever selector suits you.

How to change the text of an element in jQuery?

To change text inside an element using jQuery, use the text() method.

How to replace with jQuery?

We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.


1 Answers

You can try

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

http://jsfiddle.net/2EvGF/

You could use instead .html() and/or further sophisticate the .replace() call according to your needs

like image 174
Doug Owings Avatar answered Sep 22 '22 19:09

Doug Owings