Trying to get autoplay to work after the image is clicked












2














Right now it doesn't autoplay after it's clicked:



1.)



https://jsfiddle.net/byw5szgt/200/



This piece here is supposed to work with:



  const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));


This:



  window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
state.player = new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,


I don't know how I would get them to work.



2.) Doing just this doesn't work:



https://jsfiddle.net/byw5szgt/201/



  playerVars: {
autoplay: 1,


Inside firefox, or Chrome, outside of jsfiddle, you can clearly see there that it autoplays before the image is clicked. It starts playing behind the image.



Which is why the code would need to work with how it is set up in 1.), but I can't seem to figure out how I would do it.



html



 <div class="video" data-id="M7lc1UVf-VE"></div>


This is the full javascript code:



(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
const hide = (el) => el.classList.add("hide");
const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));

function coverClickHandler(evt) {
const cover = evt.currentTarget;
const wrapper = cover.parentNode.querySelector(".wrapg");
hide(cover);
show(wrapper);
autoplay(wrapper);
}
const cover = document.querySelector(".jacketc");
cover.addEventListener("click", coverClickHandler);
}());
(function videoPlayer() {
"use strict";

function onPlayerReady(evt) {
const player = evt.target;
player.setVolume(50); // percent
}
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/player_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
},
events: {
"onReady": onPlayerReady
}
});
};
}());









share|improve this question
























  • Can you try without using IIFE and calling the function instead.
    – Mayank Parnami
    Nov 12 '18 at 9:48
















2














Right now it doesn't autoplay after it's clicked:



1.)



https://jsfiddle.net/byw5szgt/200/



This piece here is supposed to work with:



  const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));


This:



  window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
state.player = new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,


I don't know how I would get them to work.



2.) Doing just this doesn't work:



https://jsfiddle.net/byw5szgt/201/



  playerVars: {
autoplay: 1,


Inside firefox, or Chrome, outside of jsfiddle, you can clearly see there that it autoplays before the image is clicked. It starts playing behind the image.



Which is why the code would need to work with how it is set up in 1.), but I can't seem to figure out how I would do it.



html



 <div class="video" data-id="M7lc1UVf-VE"></div>


This is the full javascript code:



(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
const hide = (el) => el.classList.add("hide");
const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));

function coverClickHandler(evt) {
const cover = evt.currentTarget;
const wrapper = cover.parentNode.querySelector(".wrapg");
hide(cover);
show(wrapper);
autoplay(wrapper);
}
const cover = document.querySelector(".jacketc");
cover.addEventListener("click", coverClickHandler);
}());
(function videoPlayer() {
"use strict";

function onPlayerReady(evt) {
const player = evt.target;
player.setVolume(50); // percent
}
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/player_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
},
events: {
"onReady": onPlayerReady
}
});
};
}());









share|improve this question
























  • Can you try without using IIFE and calling the function instead.
    – Mayank Parnami
    Nov 12 '18 at 9:48














2












2








2







Right now it doesn't autoplay after it's clicked:



1.)



https://jsfiddle.net/byw5szgt/200/



This piece here is supposed to work with:



  const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));


This:



  window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
state.player = new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,


I don't know how I would get them to work.



2.) Doing just this doesn't work:



https://jsfiddle.net/byw5szgt/201/



  playerVars: {
autoplay: 1,


Inside firefox, or Chrome, outside of jsfiddle, you can clearly see there that it autoplays before the image is clicked. It starts playing behind the image.



Which is why the code would need to work with how it is set up in 1.), but I can't seem to figure out how I would do it.



html



 <div class="video" data-id="M7lc1UVf-VE"></div>


This is the full javascript code:



(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
const hide = (el) => el.classList.add("hide");
const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));

function coverClickHandler(evt) {
const cover = evt.currentTarget;
const wrapper = cover.parentNode.querySelector(".wrapg");
hide(cover);
show(wrapper);
autoplay(wrapper);
}
const cover = document.querySelector(".jacketc");
cover.addEventListener("click", coverClickHandler);
}());
(function videoPlayer() {
"use strict";

function onPlayerReady(evt) {
const player = evt.target;
player.setVolume(50); // percent
}
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/player_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
},
events: {
"onReady": onPlayerReady
}
});
};
}());









share|improve this question















Right now it doesn't autoplay after it's clicked:



1.)



https://jsfiddle.net/byw5szgt/200/



This piece here is supposed to work with:



  const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));


This:



  window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
state.player = new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,


I don't know how I would get them to work.



2.) Doing just this doesn't work:



https://jsfiddle.net/byw5szgt/201/



  playerVars: {
autoplay: 1,


Inside firefox, or Chrome, outside of jsfiddle, you can clearly see there that it autoplays before the image is clicked. It starts playing behind the image.



Which is why the code would need to work with how it is set up in 1.), but I can't seem to figure out how I would do it.



html



 <div class="video" data-id="M7lc1UVf-VE"></div>


This is the full javascript code:



(function iife() {
"use strict";
const show = (el) => el.classList.remove("hide");
const hide = (el) => el.classList.add("hide");
const enableAutoplay = (el) => el.src.replace("autoplay=0", "autoplay=1");
const autoplay = (el) => el.setAttribute("src", enableAutoplay(el));

function coverClickHandler(evt) {
const cover = evt.currentTarget;
const wrapper = cover.parentNode.querySelector(".wrapg");
hide(cover);
show(wrapper);
autoplay(wrapper);
}
const cover = document.querySelector(".jacketc");
cover.addEventListener("click", coverClickHandler);
}());
(function videoPlayer() {
"use strict";

function onPlayerReady(evt) {
const player = evt.target;
player.setVolume(50); // percent
}
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/player_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
const videoId = video.getAttribute("data-id");
new YT.Player(video, {
width: 606,
height: 344,
videoId: videoId,
playerVars: {
autoplay: 0,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
},
events: {
"onReady": onPlayerReady
}
});
};
}());






javascript youtube youtube-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 9:49

























asked Nov 12 '18 at 9:38









htmlcoding876

135




135












  • Can you try without using IIFE and calling the function instead.
    – Mayank Parnami
    Nov 12 '18 at 9:48


















  • Can you try without using IIFE and calling the function instead.
    – Mayank Parnami
    Nov 12 '18 at 9:48
















Can you try without using IIFE and calling the function instead.
– Mayank Parnami
Nov 12 '18 at 9:48




Can you try without using IIFE and calling the function instead.
– Mayank Parnami
Nov 12 '18 at 9:48












1 Answer
1






active

oldest

votes


















0














el passed in enableAutoplay is



<div class="wrapg hide"><!---//--></div>


You need to modify src attribute for the child iframe in div.wrapg.



Querying div.wrapg for the iframe and making the modification can be accomplished like so:



const enableAutoplay = (el) => {
const widget = el.querySelector("#widget2")
widget.src = widget.src.replace("autoplay=0", "autoplay=1");
};





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259356%2ftrying-to-get-autoplay-to-work-after-the-image-is-clicked%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    el passed in enableAutoplay is



    <div class="wrapg hide"><!---//--></div>


    You need to modify src attribute for the child iframe in div.wrapg.



    Querying div.wrapg for the iframe and making the modification can be accomplished like so:



    const enableAutoplay = (el) => {
    const widget = el.querySelector("#widget2")
    widget.src = widget.src.replace("autoplay=0", "autoplay=1");
    };





    share|improve this answer


























      0














      el passed in enableAutoplay is



      <div class="wrapg hide"><!---//--></div>


      You need to modify src attribute for the child iframe in div.wrapg.



      Querying div.wrapg for the iframe and making the modification can be accomplished like so:



      const enableAutoplay = (el) => {
      const widget = el.querySelector("#widget2")
      widget.src = widget.src.replace("autoplay=0", "autoplay=1");
      };





      share|improve this answer
























        0












        0








        0






        el passed in enableAutoplay is



        <div class="wrapg hide"><!---//--></div>


        You need to modify src attribute for the child iframe in div.wrapg.



        Querying div.wrapg for the iframe and making the modification can be accomplished like so:



        const enableAutoplay = (el) => {
        const widget = el.querySelector("#widget2")
        widget.src = widget.src.replace("autoplay=0", "autoplay=1");
        };





        share|improve this answer












        el passed in enableAutoplay is



        <div class="wrapg hide"><!---//--></div>


        You need to modify src attribute for the child iframe in div.wrapg.



        Querying div.wrapg for the iframe and making the modification can be accomplished like so:



        const enableAutoplay = (el) => {
        const widget = el.querySelector("#widget2")
        widget.src = widget.src.replace("autoplay=0", "autoplay=1");
        };






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 '18 at 9:55









        Oluwafemi Sule

        10.9k1431




        10.9k1431






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259356%2ftrying-to-get-autoplay-to-work-after-the-image-is-clicked%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()