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

185
Visualizações
Find ID of next instance of a specific class

I'm trying to find the ID (test1, test2, test3 etc..) of the next instance of a specific class (findMe).

Here is my code: HTML:

    <div class="container">
  <table>
    <tr id="test1" class="findMe">
      <td>
      <button class="next">Next</button>
      </td>
    </tr>
    <tr id="test2" class="findMe">
      <td>
      <button class="next">Next</button>
      </td>
    </tr>
  </table>
    <div id="test3" class="findMe">
    </div>
  </container>

JS:

$(".next").click(function() {
console.log($(this).parent().closest(".findMe").next().attr('id'));
})

I can find the ID "test2" but not "test3". Why?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The second .findMe's nearest ancestor with the third .findMe is a grandparent, not a parent. (The <tr> is inside a <tbody>.)

While you could fix it (kinda) by making your dynamic DOM navigation more flexible, an easier method I think would be to select all .findMes, then access the next index.

const findMes = $('.findMe');
const thisIndex = findMes.index(this);
console.log(findMes[thisIndex + 1]?.id);

You could also use getElementsByClassName, whose collection is live, instead of re-selecting all elements each time.

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