Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
How to keep only the first element from a class when cloning with jquery

Each of my divs have a class attributes named remove, the first class has the id: remove_item then the second remove_item_1, the third remove_item_2, etc.

My problem is that i only want to clone first one with the id remove_item and remove all the other one from the clone.

If i do clone.find('.remove'); i am able to gather all the elements with remove class but from there i am kinda lost on how to do that.

Could anyone help ?

Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can keep first div by adding :first.

clone.find('.remove:first');
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think I got what you mean

here's an example how to pick the first element of a cloned div:

lets say you have

<ul id="list">
        <div id="clone">
            <li class="remove remove1">Remove 1 </li>
            <li class="remove remove2">Remove 2</li>
            <li class="remove remove3">Remove 3</li>
        </div>
    </ul>

The script to clone the list and remove the first child of the clone goes like this :

<script>
        let clone = $('#clone').clone().appendTo('#list');
        clone.children().first().remove();
    </script>

or you can select the first one by class selection as @Manashree Shah mentioned like this:

clone.find('.remove:first');

The code uses jQuery, you can add this before the script if you haven't already added it :

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda