I am using Javascript to make a video player that plays a video. All the codes seem to be added right. But the code does not seem to work. Here it is:
HTML
<video id="myVideo">
<source src="coolfirst2hours.mp4" type="video/mp4">
</video>
<button id="playVid" type="button">PLAY</button>
Javascript
// Get the video and play button elements
const videoElem = document.getElementById('myVideo');
const playButton = document.getElementById('playVid');
// Add event listener to play button
playButton.addEventListener('click', () => {
videoElem.play();
});
2
Answers
For the end of your HTML i think i will add this, for define buttons :
And for the JavaScript part :
you can try to check a couple of possible errors:
Ensure that the path to coolfirst2hours.mp4 is correct, i.e. at the
same level as your HTML file
You can wrap your JavaScript code in a DOMContentLoaded event to
ensure it waits until the page is fully loaded
});