Reputation: 1
Whenever i try press the buttons they only work for 1 of the videos instead of each set being used for the intended video. Any help will be appreciated. And by the way i'm usign google chrome.Thanks:)
<div style="text-align:center">
<button onclick="playPause()">Play/Pause</button>
<button onclick="makeBig()">Big</button>
<button onclick="makeSmall()">Small</button>
<button onclick="makeNormal()">Normal</button>
<br>
<video id="myVideo1" width="420">
<source src="r8_ext.mp4" type="video/mp4">
</video>
</div>
<script>
var myVideo = document.getElementById("myVideo1");
function playPause() {
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
function makeBig() {
myVideo.width = 560;
}
function makeSmall() {
myVideo.width = 320;
}
function makeNormal() {
myVideo.width = 420;
}
</script>
<div style="text-align:center">
<button onclick="playPause()">Play/Pause</button>
<button onclick="makeBig()">Big</button>
<button onclick="makeSmall()">Small</button>
<button onclick="makeNormal()">Normal</button>
<br>
<video id="myVideo2" width="420">
<source src="r8_int.mp4" type="video/mp4">
</video>
</div>
<script>
var myVideo = document.getElementById("myVideo2");
function playPause() {
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
function makeBig() {
myVideo.width = 560;
}
function makeSmall() {
myVideo.width = 320;
}
function makeNormal() {
myVideo.width = 420;
}
</script>
Thanks:)
Upvotes: 0
Views: 42
Reputation: 97672
You're using the same variable and functions for both videos. Use a unique variable for each video and let the functions take the video as an argument instead of duplicating them.
var myVideo1 = document.getElementById("myVideo1");
var myVideo2 = document.getElementById("myVideo1");
...
function playPause(video) {
if (video.paused)
video.play();
else
video.pause();
}
...
<button onclick="playPause(myVideo1)">Play/Pause</button>
...
<button onclick="playPause(myVideo2)">Play/Pause</button>
Upvotes: 1
Reputation: 1857
You are using the same variable name in both scripts, 'myVideo'. They need to be unique. Also, you have duplicate functions. You should refactor them to avoid duplication.
Upvotes: 0