Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery if div contains this text, replace that part of the text

Like the title says, I want to replace a specific part of the text in a div.

The structure looks like this:

<div class="text_div">     This div contains some text. </div> 

And I want to replace only "contains" with "hello everyone", for example. I can't find a solution for this.

like image 331
Daniel Avatar asked Jul 04 '12 07:07

Daniel


People also ask

How to change text of element in jQuery?

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

How do I find a word in a string using jQuery?

How to find if a word or a substring is present in the given string. In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false.

How use contains in jQuery?

jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).

How to replace the text of any element using jQuery?

To replace the text of any element using jQuery use the text function together with the replace function of JavaScript. For example: $ ("#element").text ($ ("#element").text ().replace ("Hi", "Bye"));. Here's the step by step process: Find and select the element containing the text.

What is jQuery replace Div contents?

The jQuery replace div contents is the data contents that can be used on the tag and replacing the contents wherever we needed using jQuery method like replaceWith () or any other replace methods we want to replace the datas that can be covered on the innerHTML using the div element or any other html elements. The data contents are declare...

How do I replace text in a div with another string?

You can use the text method and pass a function that returns the modified text, using the native String.prototype.replace method to perform the replacement: $ (".text_div").text (function () { return $ (this).text ().replace ("contains", "hello everyone"); });​​​​​ Here's a working example.

How to get the text of an element using jQuery?

Here's the step by step process: 1 Find and select the element containing the text. 2 Use the jQuery text function to get its text. 3 Replace the text using replace. 4 Pass the result of this process to the text function to the same element. More ...


1 Answers

You can use the text method and pass a function that returns the modified text, using the native String.prototype.replace method to perform the replacement:

​$(".text_div").text(function () {     return $(this).text().replace("contains", "hello everyone");  });​​​​​ 

Here's a working example.

like image 70
James Allardice Avatar answered Nov 15 '22 22:11

James Allardice