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

191
Visualizações
How can I apply javascript to several Elements with different IDs at once?

I have this code that allows me to stop YouTube videos from playing once a button is pressed. Here is an example:

    //Hotel Post
<script>
  $(document).ready(function() {
    // set unique id to videoplayer for the Webflow video element
    var src = $('#videoplayer').children('iframe').attr('src');

    // when object with class open-popup is clicked...
    $('.open-popup.left').click(function(e) {
        e.preventDefault();
        // change the src value of the video
        $('#videoplayer').children('iframe').attr('src', src);
        $('.popup-bg').fadeIn();
    });

    // when object with class close-popup is clicked...
    $('.close-modal-button').click(function(e) {
        e.preventDefault();
        $('#videoplayer').children('iframe').attr('src', '');
        $('.popup-bg').fadeOut();
    });
  });
</script>

//Gans to go
<script>
  $(document).ready(function() {
    // set unique id to videoplayer for the Webflow video element
    var src = $('#videoplayer2').children('iframe').attr('src');

    // when object with class open-popup is clicked...
    $('.open-popup.left').click(function(e) {
        e.preventDefault();
        // change the src value of the video
        $('#videoplayer2').children('iframe').attr('src', src);
        $('.popup-bg').fadeIn();
    });

    // when object with class close-popup is clicked...
    $('.close-modal-button').click(function(e) {
        e.preventDefault();
        $('#videoplayer2').children('iframe').attr('src', '');
        $('.popup-bg').fadeOut();
    });
  });
</script>

I have about 14 videos like this embedded on my site and it makes things slow, because I have one of these scripts for every video. Is there any way I can select several ids at once or something? So I have one script for all videos?

I would appreciate the help. Thanks a lot. Pablo

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

0

One way is to set all the videos to have a common className, like video and store the variable src into a data-attribute.

$(document).ready(function() {
      // set unique id to videoplayer for the Webflow video element

      $('.video').each(function() {
        let src = $(this).children('iframe').attr('src');
        $(this).attr('data-src', src);
      })

      // when object with class open-popup is clicked...
      $('.open-popup.left').click(function(e) {
            e.preventDefault();
            // change the src value of the video
            $('.video').each(function() {
              $(this).children('iframe').attr('src', $(this).data('src'));
              $('.popup-bg').fadeIn();
            });

            // when object with class close-popup is clicked...
            $('.close-modal-button').click(function(e) {
              e.preventDefault();
              $('.video').each(function() {
                $(this).children('iframe').attr('src', '');
                $('.popup-bg').fadeOut();
              });
            });

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