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

124
Visualizações
JQuery - Function applying only for first element with class

I'm trying to update a modal & video attributes on button click.

It works well but the problem is that it only apply this when i click on the first button ( first element with the class videoBtn ), i want it to be applied on all other buttons with the same class

here is my code

$(".videoBtn").click(function(){
        $("#video").attr('src',$(".videoBtn").data('src'));
        $(".modal").attr('id', $(".videoBtn").data('target').substring(1));
      });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are not using value of currently clicked element, but first with class, you should use $this instead of $('.videoBtn')

$(".videoBtn").click(function(){
   $("#video").attr('src',$(this).data('src'));
   $(".modal").attr('id', $(this).data('target').substring(1));
});
about 4 years ago · Juan Pablo Isaza Relatório

0

$(".videoBtn").each(function(){
  $(this).on('click', function() {
    $("#video").attr('src',$(".videoBtn").data('src'));
    $(".modal").attr('id', $(".videoBtn").data('target').substring(1));
  })
})
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