/    Sign up×
Community /Pin to ProfileBookmark

on click play youtube video

I found a script that allows to plays the youtube video on one click. The reason for this is so I can have a custom image on top of the youtube video.

When the user clicks on the video (which is an image placed on top of the video) the image disappears and the youtube video starts to play…

Here is the code

HTML and javascript

[code=html]<div id=”vid1″>
<a href=”javascript:void(0);” onclick=”play();”>
<img id=”thumb” onClick=”document.getElementById(‘thumb’).style.display=’none'” src=”video.jpg” />
</a>

<div id=”ytapiplayer”></div>
<script type=”text/javascript”>

var atts = { id: “myytplayer” };
swfobject.embedSWF(“http://www.youtube.com/video”,
“ytapiplayer”, “640”, “480”, “8”, null, null, params, atts);

</script>

</div>[/code]

Javascript:

[CODE] function updateHTML(elmId, value) {
document.getElementById(elmId).innerHTML = value;
}

function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById(“myytplayer”);
setInterval(updateytplayerInfo, 250);
updateytplayerInfo();
ytplayer.addEventListener(“onStateChange”, “onytplayerStateChange”);
ytplayer.addEventListener(“onError”, “onPlayerError”);
}

function play() {
if (ytplayer) {
ytplayer.playVideo();
}
}

[/CODE]

How can I add two videos on one page? I can only get one to work on a page and not two (or more)

I tried this code

[CODE]
function updateHTML(elmId, value) {
document.getElementById(elmId).innerHTML = value;
}

function onYouTubePlayerReady(playerId) {
ytplayer2 = document.getElementById(“myytplayer2”);
setInterval(updateytplayerInfo, 250);
updateytplayerInfo();
ytplayer2.addEventListener(“onStateChange”, “onytplayerStateChange”);
ytplayer2.addEventListener(“onError”, “onPlayerError”);
}

function play() {
if (ytplayer2) {
ytplayer2.playVideo();
}
}[/CODE]

I have also changed the id in the HTML/javascript shown above…
I can get both videos to work separately but not together. By this I mean when I click on video 1 the image disappears and the video plays…
But if I click on video 2, the image disappears and I have to click again after the image disappears to play the video – it doesn’t play on the one click…

Hope this makes sense.

Can anyone help?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @pc_mac spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...