Let me start by saying, sorry for the confusing wording of the title, I wasn't sure how to state my issue.
I am using the jQuery ".on" function to handle three events for a specific element/selector (.custom-video-image), like so:
$('.custom-video-image').on({
'click': function() {
video = '<iframe src="'+ $(this).attr('data-video') +'"></iframe>';
$(this).siblings('.custom-play').css('display', 'none');
$(this).parent().toggleClass("image-wrapper video-wrapper");
$(this).replaceWith(video);
},
'mouseover': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-color.png");
},
'mouseout': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-dark-trans.png");
}
});
As you can see ".custom-video-image" has three events. However, I want to add multiple elements/selectors to this function but not globally. For example, I am not looking to attach the three events to the additional elements/selectors, which would be achieved like so:
$('.custom-video-image, .selector-2, .selector-3').on({
'click': function() {
video = '<iframe src="'+ $(this).attr('data-video') +'"></iframe>';
$(this).siblings('.custom-play').css('display', 'none');
$(this).parent().toggleClass("image-wrapper video-wrapper");
$(this).replaceWith(video);
},
'mouseover': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-color.png");
},
'mouseout': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-dark-trans.png");
}
});
What I am looking to achieve is apply the two additional elements/selectors to the "mouseover" and "mouseout" events. The following code is simply for illustrative purposes - this is what I am trying to achieve:
$('.custom-video-image').on({
'click': function() {
video = '<iframe src="'+ $(this).attr('data-video') +'"></iframe>';
$(this).siblings('.custom-play').css('display', 'none');
$(this).parent().toggleClass("image-wrapper video-wrapper");
$(this).replaceWith(video);
},
$('.custom-video-image, .selector-2, .selector-3').on({
'mouseover': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-color.png");
},
$('.custom-video-image, .selector-2, .selector-3').on({
'mouseout': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-dark-trans.png");
}
});
Again, I know that last code sample isn't functional, I just wanted to illustrate what I am trying to achieve in one ".on" function - if that is even possible.
Finally, I know I can achieve this by creating multiple ".on" function calls or various other approaches but I wanted to see if it was possible using this method to specify additional elements/selectors for specific events in an ".on" function.
Any help is greatly appreciated, if there is an alternative solution to reducing the required lines of code to achieve the same effect, I am all ears! Simply trying to learn more effective and efficient ways.
Thank you.
Edit 1: If no alternative solution presents itself, I will be resorting to the following solution (I am open to any suggestions which better the following code):
$('.custom-video-image').on({
'click': function () {
video = '<iframe src="' + $(this).attr('data-video') + '"></iframe>';
$(this).siblings('.custom-play').css('display', 'none');
$(this).parent().toggleClass("image-wrapper video-wrapper");
$(this).replaceWith(video);
}
});
$('.custom-video-image, .custom-play, .banner-overlay__copy').on({
'mouseover': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-color.png");
},
'mouseout': function() {
$(this).siblings('.custom-play').attr("src","images/youtube-play-dark-trans.png");
}
});
.on()calls? What various other approaches are not desirable and why? Maybe chaining jQuery'sadd()would be helpful, but I'm not sure what benefits it offers.