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

130
Visualizações
how to handle jquery event binding properly?

I have a filter and load more listing page, individually filter and load more working, but what I actually want that is not working, I think it will be clear if I show you the code to explain my situation, here is so far what I have tried.

HTML:

<div class="block-wrap">

<ul class="filter-cats">
<li><a href="#" data-filter="all">All</a></li>
<li><a href="#" data-filter="cat">Cats</a></li>
<li><a href="#" data-filter="dog">Dogs</a></li>
</ul>
<div class="animal-listing" id="list">
  <div>Cat 1</div>
  <div>Cat 2</div>
  <div>dog 1</div>
  <div>Cat 3</div>
  <div>dog 2</div>  
</div>
<button class="load-more" data-filter-id="all">Load More</button>
  
</div>

And the jQuery part is this:

$(function() {
  var el = $('.filter-cats').find('a');
  el.on('click', function (e) {    
        e.preventDefault();
        var currentFilter = $(this),
        catId = $(currentFilter).data('filter');
        $('.load-more').attr('data-filter-id', catId);  
    });
  
   $('.load-more').on('click',  function () {
        var ct_filtr = $(this).data('filter-id');
        console.log(ct_filtr);
    })
})

Issue I am facing is, when I click any filter, let's say "Cats", it sets "data-filter-id" successfully on the button with load-more class, I can see that changes when inspecting the element. But after that when I click on the Load More button, it is not giving me the changed "data-filter-id" value, giving me the first clicked filter id only. I know this is something to do with data binding, I am not a developer, so how can I get this work correctly, any insights will be greatly appreciated.

In summary, when I click on any filter category, that should set the Load More button data-filter-id and when I am clicking on the load more button I need to get that value to pass to another function.

Here is the codepen link: https://codepen.io/salih24by7/pen/LYjRxXL

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

0

You can use event delegation and attach a single listener to the parent container of your filter options rather than to each separate link:

$('.filter-cats').on('click', 'a', e => {
  e.preventDefault();
  var d = $(e.target).attr('data-filter');
  $('.load-more').attr('data-filter-id', d);
});

$('.load-more').on('click', e => {
    console.log($(e.target).attr('data-filter-id'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="filter-cats">
  <li><a href="#" data-filter="all">All</a></li>
  <li><a href="#" data-filter="cat">Cats</a></li>
  <li><a href="#" data-filter="dog">Dogs</a></li>
</ul>

<div class="animal-listing" id="list">
  <div>Cat 1</div>
  <div>Cat 2</div>
  <div>dog 1</div>
  <div>Cat 3</div>
  <div>dog 2</div>  
</div>

<button class="load-more" data-filter-id="all">Load More</button>

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