Green Computers Green Computers - 4 months ago 9
Javascript Question

How to find a root element that contains specific text using nested id

I would like to select a root element that contains a specific text using nested div id in Query. For example:

<div>...
<div id="parentDiv1">
<span>
....
<span>
.....
<div>..</div>
<div id="myId1">...</div>
</span>
....
</span>
</div>
</div>

<div id="parentDiv2">
<span>
....
<h2>..</h2>
<span>
.....
<div>..</div>
<div id="myId2">...</div>
</span>
....
</span>
</div>


Now, I need to select a div which has the id
parentDiv
with a nested div having id
#myId1
.

I find more difficult in this case. I know this is a complex case. Can anybody help me please.

Answer

Assuming I'm understanding the question correctly, this should work:

$("div:contains('parentDiv')").has("#myId1");

or if you need just the id of the div with those attributes:

$("div:contains('parentDiv')").has("#myId1").attr('id');

For more info check out:

Jquery :contains selector

Comments