Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda