I have got this structure:
<table id="thetable">
<tr>
<td>
<div>
<div>some text</div>
<div>
<div></div>
<div>(This text is good)</div>
<div>some text2</div>
<div>some text3</div>
</div>
</div>
</td>
<tr>
</tr>
<td>
<div>
<div>some text</div>
<div>
<div></div>
<div>(This text is good)</div>
<div>some text2</div>
<div>some text3</div>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div>
<div>some text</div>
<div>
<div></div>
<div>(This text is good)</div>
<div>some text2</div>
<div>some text3</div>
</div>
</div>
</td>
</tr>
</table>
I want to take the text inside the div, but only the text with this word: good. and replace. So I am doing this:
$("#thetable div:contains('good')").each(function () {
try {
console.log($(this).text());
console.log("------------------------------");
} catch (ex) {
}
})
But the console.log is saying to me that each log is taking not only the text of the div with good text but also the other two. It is printing:
(This text is good)some text2some text3
So I want to take only the div with text good and replace this text with another text.
Thanks!
Basically: I want to get the div where I have got the word "good"(or any word I want), and in this div insert/replace the text. Find in all document or, specifically, in a part of the document, like this example: in a table.
Because you have a div which contains those 3 divs
one possible solution is to fetch only the end elements like
$("#thetable div:contains('good'):not(:has(*))").each(function () {
})
Demo: Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With