I have run into a small issue when trying to attach event handlers to elements on a page.
The elements I am attaching the handlers too are dynamical created at runtime and some work with the events and some do not.
For example the following work well:
// Setup full screen button
fullscreenButton = document.createElement("img");
fullscreenButton.src = "/media/site-images/fullscreen.svg";
fullscreenButton.setAttribute("class", "fullscreenButton");
$(fullscreenButton).on("click", (function (video) {
return function () {
if (video.requestFullScreen) {
video.requestFullScreen();
} else if (video.webkitRequestFullScreen) {
video.webkitRequestFullScreen();
} else if (video.mozRequestFullScreen) {
video.mozRequestFullScreen();
}
};
}(this)));
The video goes to fullscreen fine. However when I do something like this:
// Setup the div container for the video
videoContainer = document.createElement("div");
videoContainer.setAttribute("class", "videoContainer");
$(this).wrap(videoContainer);
// When the hover leaves, hide the controls
$(videoContainer).on("mouseleave", (function (controlsBox) {
return function () {
$(controlsBox).fadeTo(400, 0);
$(controlsBox).clearQueue();
};
}(controlsBox)));
// If the video itself is clicked, play/pause the video
$(videoContainer).on("click", (function (vid, playPauseButton) {
return function () {
playPause(vid, playPauseButton);
};
}(this, playPauseButton)));
No event is fired.
I have read through and used links like .on() jquery not working to get around it with some success but I am confused as to the difference on why one dynamic element works with event handlers and other dont.
A JSfiddle to the whole shebang is here: http://jsfiddle.net/m4twG/1/ (obviously a work in progress)