Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
How to select child element based on parent element using java script query selector

my page multiple element have same class like below

<div>
 <ol class="sample-class">
  <li>list1</li>
  <li>list2</li>
  <li>list3</li>
 </ol>
 <div class="help">condent 1</div>
</div>
....
</div>
 <ol class="sample-class">
   <li>list1</li>
   <li>list2</li>
   <li>list3</li>
  </ol>
 <div class="help">condent 2</div>
</div

I need to change the help content based on sample-class I js to select the element but it is not working.

let listVal = document.getElementsByClassName('sample-class');
for(let el of listVal) {
 el.offsetParent.getElementsByClassName('help')[0].innerHTML = "my dynamic condent"
}

anyone help me. Thanks

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You cannot use getElementById to find something inside another element, so you need to use getElementByClassName instead. Also, as mentioned in one of the comments, the ID should be a unique. No 2 elements can have the same ID.

Kindly check the below snippet which would work as expected.

let listVal = document.getElementsByClassName('sample-class');
for(let el of listVal) {
 el.getElementsByClassName('help')[0].innerHTML = "my dynamic condent"
}
<div>
    <div class="sample-class">
        <ol>
            <li>list1</li>
            <li>list2</li>
            <li>list3</li>
        </ol>
        <div class="help">condent 1</div>
    </div>
    <div class="sample-class">
        <ol>
            <li>list1</li>
            <li>list2</li>
            <li>list3</li>
        </ol>
        <div class="help">condent 2</div>
    </div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can create a new querySelector in the for loop to select the element with the help class/id, you can achieve this by doing the following:

1 By using parentElement

const elements = document.querySelectorAll(".sample-class");
for (const element of elements) {
  element.parentElement.querySelector(".help").innerText = "new text"
}

2 By using sibling selector

const elements = document.querySelectorAll(".sample-class");
for (const element of elements) {
  element.querySelector("~ .help").innerText = "new text"
}
about 4 years ago · Juan Pablo Isaza Report

0

You have overcomplecated your code by looping over an array and not modifing the array you are looping over. I also like to state that when accessing getElementById you always get the first DOM element containing this ID. To fix this issue change the id's to classes and loop over these classes instead of the siblings

const elements = document.querySelectorAll(".help");
for (const element of elements) {
  element.innerText = "new text"
}

If for some reason you need to loop over the ol elements:

const elements = document.querySelectorAll("ol");
for (const element of elements) {
  element.nextSibling.innerText = "new text"
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!