Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GodziLaravel's avatar

Jquery: How to find a child (not direct child) of a specific element by Id (Parent Id)?

Hello ,

<parent id="abc-1">

  <child-1>
    <child-2></child-2>
    	<child-3>
    		<child-4 class="my-class">Hey my data</child-4>
	</child-3>
   </child-1>

  <child-5></child-5>

</parent>

I would like to get the content of the element where the class name is my-class

This element exists inside parent with the id="abc-1" but we don't know on which level, maybe child or child of child or ... but we are sure that the class name is my-class

0 likes
3 replies
GodziLaravel's avatar

@nakov I corrected the title, I mean the parent Id.

it's like : give me the element with class name is my-class where its parent (or parent of parent or ...) does have this id="abc-1"

Nakov's avatar
Nakov
Best Answer
Level 73

Then this should do it:

document.querySelector(".my-class").closest("#abc-1")

Please or to participate in this conversation.