Video addEventListener of a dynamically created element

Do you have an idea why I get the error "Unable to create event listener from null":

var my; my.newVidObj = document.createElement('video'); my.newVidObj.src = "vid-source.webm"; my.newVidObj.load(); my.newVidObj.addEventListener("play", function() { // Do something }, false); 

Also, is there a way to use video tag methods on jquery objects (e.g. creating a video tag via jquery)?

I edited the answer below to be correct, but it should be checked by experts. Here is the solution:

 var vid = $("<video />", { id: "my-HTML5-video", src: "video.webm" }).bind("play", function(){ alert('test'); }).appendTo("body")[0].play(); 
+4
source share
1 answer

Check out these links:

http://www.chipwreck.de/blog/2010/03/01/html-5-video-dom-attributes-and-events/ http://www.dev.opera.com/articles/view/introduction- html5-video / http://www.chipwreck.de/blog/2010/02/23/html-5-video-test-area/

I think it should be:

 var vid = $("<video />", { id: "my-HTML5-video", src: "vid-source.webm", }); vid.appendTo("body"); vid[0].play = function(){ alert("yaaay!"); }; 

OP correction:

 var vid = $("<video />", { id: "my-HTML5-video", src: "video.webm" }).bind("play", function(){ alert('test'); }).appendTo("body")[0].play(); 
+3
source

Source: https://habr.com/ru/post/1343870/


All Articles