You say by class name, while in the title is by ID.. here is how you get elements in javascript by class name: https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp
let elements = document.getElementsByClassName('my-class');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Then this should do it:
document.querySelector(".my-class").closest("#abc-1")
Please or to participate in this conversation.