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

187
Vistas
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 Respuestas
Responde la pregunta

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 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