I inserted a video on the site, you need to make a play and pause button for the video, so that when you close the modal window, the video does not play, but for some reason this does not work, here is the code I used:

html:

<video id="video1" src="video/Nyan-Cat-video.mp4" controls></video> <button onclick="playPause()">Play/Pause</button> 

javascript:

 var myVideo = document.getElementById("video1"); function playPause() { if (myVideo.paused) $('#video1').get(0).play() else $('#video1').get(0).pause() } 
  • one
    correct myVideo.play() w3schools.com/tags/… from here w3schools.com/tags/av_met_play.asp - Jean-Claude
  • I will write down on a subject how all the same to solve a problem. The playPause () function must be placed outside the doc ready body. as well as defining a variable in a video var myVideo = document.getElementById ("video1"); then onclick will work. (there was also an individual error of my code (js file crashed)) - maggot brain
  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

Another solution is taken from here https://stackoverflow.com/questions/17378199/uncaught-referenceerror-function-is-not-defined-with-onclick

They write: never use .onclick () from the document.

Then the updated code will be:

 <!DOCTYPE html> <html> <body> <button type="button" id="butPlay"> Play Video </button> <video id="myVideo" src="http://www.w3schools.com/tags/mov_bbb.mp4" width="320" height="176" controls></video> <script> document.getElementById("butPlay").addEventListener("click", playPause, false); function playPause() { var myVideo = document.getElementById("myVideo"); if (myVideo.paused) myVideo.play(); else myVideo.pause(); } </script> </body> </html> 

  • for some reason it still does not work, this time even without any errors - maggot brain
  • I will write down on a subject how all the same to solve a problem. The playPause () function must be placed outside the doc ready body. as well as defining a variable in a video var myVideo = document.getElementById ("video1"); then onclick will work. (there was also an individual error of my code (the js file crashed)) - maggot brain
  • @maggotbrain mdee ..... you are the basics for urgently learning ........ because the functions and functions are that they must be declared before any construction of a tree house ............ Moreover, the example from Jean-Claude clearly shows that there are no DomReady ............ ohhhh ....... - Alexey Shimansky