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

165
Vistas
change a div content again and again without losing previous html

I am writing a jquery function to filter products by type. It works perfectly fine but when I filter by type more than once. Html Element of product-box named addToWishlist stop working. otherwise all products are displayed perfectly fine. Cant figure out where is the problem. Here is the code

//load products data in array         
             var productArray = [];
            $("#product-items .col-4").each (function (){
             productArray.push($(this)) })
            
        $(".filter-btn").click(function(e) {
            var btnId = e.target.id;
            var tempArray = [];
            for(var i = 0;i < productArray.length; i++){
              var type = $(productArray[i]).find('.addToWishlist').data("type");
                if(btnId == "fairness-soaps" && type == "Fairness")
                  tempArray.push(productArray[i])  
                if(btnId == "deep-clean-soaps" && type == "Deep-Clean")
                  tempArray.push(productArray[i])    
                if(btnId == "skin-whitening-soaps" && type == "Skin-Whitening")
                  tempArray.push(productArray[i])       
            }
            $("#product-items").html(tempArray);
    });

<div class="row" id="product-items">
                 <div class="col-4">
                   <a href="#">
                     <div class="product-box">
                       <div class="product-img">
                       <img src="images/product-img13.png" alt="">
                         <a type="button" class="addToWishlist" data-id="13" data-image="images/product-img13.png" data-price="$30"
                          data-name="Aloe Vera Soap" data-quantity="1" data-weight="50g" data-availability="In Stock" data-type="Fairness">
                         <i class="wishlist-icon fa fa-heart-o"></i></a>
                       </div>
                     <p class="product-name">Aloe Vera Soap</p>
                     <p class="product-price">$30</p>
                   </div>
                 </a>
               </div>
and so on....
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're not reordering the product Elements, you're rewriting them. Even though the HTML of the products is the same, the events you attach to them is lost when you call $("#product-items").html(tempArray);. You either need to reapply the events to the "addToWishList" buttons or reorder the elements instead of writing directly to the html.

Here's a contrived example that shows how just because the HTML is the same doesn't mean the event stays:

<div id="container">
    <button id="special-button">Click me</button>
</div>
<script>
    $("#special-button").on("click", function(){
        alert("I've Been clicked!");
    });
    $("#container").html(`<button id="special-button">Click me</button>`);
</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