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

166
Visualizações
How to group elements with JQuery according to levels

I want to group the following html:

<h2>Summarize the problem<h2>
<h3>Include details about your goal<h3>
<h3>Include details about your goal<h3>
<h3>Include any error messages<h3>

<h2>Describe What you have tries<h2>
<h3>Show what you’ve tried and tell us what you found<h3>
<h3>Lorem Ipsum LOL<h3>

...

into


<div>
  <h2>Summarize the problem</h2>
  <h3>Include details about your goal</h3>
  <h3>Include details about your goal</h3>
  <h3>Include any error messages</h3>
</div>

<div>
   <h2>Describe What you have tries</h2>
   <h3>Show what you’ve tried and tell us what you found</h3>
   <h3>Lorem Ipsum LOL</h3>
</div>

What I have tried

I tried looping through all the elements, but never worked. Im not putting the code here as I found it is not even making any sense

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

0

Here is a working version.

$("h2").each(function() { 
  const $this = $(this)
  const $h3 = $this.nextUntil("h2"); // find all tags until next H2
  $this
    .wrap('<div class="wrapper" />')
    .after($h3)
})
.wrapper { border: 1px solid black }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>Summarize the problem</h2>
<h3>Include details about your goal</h3>
<h3>Include details about your goal</h3>
<h3>Include any error messages</h3>

<h2>Describe what you have tried</h2>
<h3>Show what you've tried and tell us what you found</h3>
<h3>Lorem Ipsum LOL</h3>

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