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

114
Visualizações
Finding the next Canvas id in Jquery

When looking for the next element id in Jquery the simplest solution is to use closest(element). but it is not working for Canvas and I don't know why.

$('a.findNext').click(function() {
  debugger;
  var nextSectionWithId = $(this).closest("canvas").nextAll("canvas[id]:first");
  if (nextSectionWithId) {
    var sectionId = nextSectionWithId.attr('id');
    $("#test").text(sectionId)
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="section_1">
  <a href="#" class="findNext">Find</a>
</div>
<div></div>
<canvas id="section_3"></canvas>
<canvas id="section_4"></canvas>

<div id='test'></div>

Demo : http://jsfiddle.net/jtdgo304/6/

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

0

.closest will select the nearest ancestor matching the selector. But the .findNext element does not have a canvas ancestor.

If you want to get the next ancestor, you'll need to first navigate to an element that's a sibling of the canvas (which is the #section_1 here), then use .nextAll.

You should also check the .length of the jQuery collection to see if it matches any elements.

$('a.findNext').click(function() {
  const nextSectionWithId = $(this).parent().nextAll("canvas[id]:first");
  if (nextSectionWithId.length) {
    const sectionId = nextSectionWithId.attr('id');
    $("#test").text(sectionId)
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="section_1">
  <a href="#" class="findNext">Find</a>
</div>
<div></div>
<canvas id="section_3"></canvas>
<canvas id="section_4"></canvas>

<div id='test'></div>

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