I have this piece of code $('video').on('contextmenu', function(e) {e.preventDefault();});
to prevent the right click menu on the video tag. However it does not seem to work on dynamically added videos. Using ilightbox for the lightbox which adds the video element to the dom when one of the lighbox images is clicked. What can be done to allow this to work with dynamic elements?
Asked
Active
Viewed 798 times
2

user1445852
- 67
- 4
-
if contextmenu event bubbles, use delegation... – A. Wolff Sep 11 '13 at 17:24
-
possible duplicate of [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – Jason P Sep 11 '13 at 17:26
-
$("video").on("contextmenu", function(){}); is equivalent to $("video").bind("contextmenu", function(){}). If the video tag doesn't exist in the DOM at that point, you won't be adding the event handler to that element. – Kerry Liu Sep 11 '13 at 17:30
2 Answers
0
At first try to delegate event to body level:
$(document.body).on('contextmenu','video', function(e) {e.preventDefault();});

A. Wolff
- 74,033
- 9
- 94
- 155