Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find DOM child nodes only within a specific div with a give class name?

Tags:

javascript

I have a hierarchical div section as follows:

<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b1</button>
    </div>
    <div class="class4">
        <p>first text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b2</button>
    </div>
    <div class="class4">
        <p>second text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b3</button>
    </div>
    <div class="class4">
        <p>third text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b4</button>
    </div>
    <div class="class4">
        <p>fourth text</p>
    </div>
</div>

When I click on any button, say button b3, I want to get the text third text in javascript, not jquery.

So in my bclick() function, I am getting to the root element as follows:

function bclick(obj) {
    let class1obj = obj.parentNode.parentNode;
    // don't know what to do next to get to class 4 and then get the <p> text.
}

I want to get into class4 to get the content of <p> tag, in this case for b3 text I want is third text.

Not sure how to do. Since there a multiple occurrence of a class, I want to get only a specific class which contains the button clicked.

like image 726
user3243499 Avatar asked Oct 15 '25 02:10

user3243499


2 Answers

You can use Element.querySelector() on parent and select the <p> element inside .class4 for each parent element.

obj.parentNode.parentNode.querySelector('.class4 p')

See the working snippet

function bclick(elm){
  const parent = elm.parentNode.parentNode;
  console.log(parent.querySelector('.class4 p').innerHTML)
} 
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b1</button>
    </div>
    <div class="class4">
        <p>first text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b2</button>
    </div>
    <div class="class4">
        <p>second text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b3</button>
    </div>
    <div class="class4">
        <p>third text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b4</button>
    </div>
    <div class="class4">
        <p>fourth text</p>
    </div>
</div>
like image 167
Maheer Ali Avatar answered Oct 17 '25 19:10

Maheer Ali


You can use Document​.query​Selector() on the parent node:

The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.

function bclick(obj) {
  let class1obj = obj.parentNode.parentNode.querySelector('.class4 > p');
  console.log(class1obj.textContent);
}
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b1</button>
    </div>
    <div class="class4">
        <p>first text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b2</button>
    </div>
    <div class="class4">
        <p>second text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b3</button>
    </div>
    <div class="class4">
        <p>third text</p>
    </div>
</div>
<div class="class1">
    <div class="class2">
        <button class="class3" onclick="bclick(this)">b4</button>
    </div>
    <div class="class4">
        <p>fourth text</p>
    </div>
</div>
like image 22
Mamun Avatar answered Oct 17 '25 21:10

Mamun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!