0

I need to play the video on "Play" button click on a site

I tried using this code but it doesn't trigger the play on the Youtube Video.

jQuery(document).on('click', '.eg-henryharrison-element-2', function(event) { 
  event.preventDefault(); 
  jQuery(".esg-youtubevideo").click(); 
});
KC Chai
  • 1,607
  • 9
  • 30
  • 59

3 Answers3

3

You should try adding autoplay=1 option to your iframe src like this

<iframe width="420" height="345" src="https://www.youtube.com/embed/qYcSef6Mk58?version=3&enablejsapi=1&html5=1&controls=1&autohide=1&rel=0&showinfo=0&fs=1&playsinline=1&autoplay=1" frameborder="0" allowfullscreen></iframe>

Here is a related question already been asked at stack overflow: YouTube Iframe embed auto play

Inderjeet Singh
  • 556
  • 1
  • 9
  • 22
  • Hi Goldy, Thanks for your reply. Unfortunately, I'm not asking about autoplaying the video. – KC Chai Nov 21 '18 at 05:03
  • 1
    You can do it this way $('.esg-youtube-frame').each(function(){ var src = $(this).data('src'); src += "&autoplay=1"; console.log(src); $(this).data('src',src); }); – Inderjeet Singh Nov 22 '18 at 11:35
2

Maybe I don't understand what you actually want to achieve, but:

For me the following works:

$('.esg-click-to-play-video').first().click()

See here:

Before: Before

After:

After

Gutelaunetyp
  • 2,144
  • 4
  • 15
  • 40
1

This should work

$('.esg-youtube-frame').each(function(){ 
    var src = $(this).data('src'); 
    src += "&autoplay=1"; 
    $(this).data('src',src); 
});
Inderjeet Singh
  • 556
  • 1
  • 9
  • 22