Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of text inside each div depending text

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.

like image 993
Za7pi Avatar asked Dec 25 '22 10:12

Za7pi


1 Answers

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

like image 54
Arun P Johny Avatar answered Jan 11 '23 22:01

Arun P Johny