0

I am trying to condense my code in order to implement the DRY principle, I have played around with the code a bit and manage to condense some of it but not all.

The area I am looking to condense is the lines with the code:

$("#modal").on("hidden.bs.modal", function() {}

As you can see this is repeated throughout the file and I do not believe this is best practice. I have tried to place this in a function itself but the problem I have is that when I close the video it still continues to play.

Not sure if the code can be reduced any further so thought I would ask for advice.

// Video Play
$(function() {
 // Auto Play Modal Video
 $(".video").click(function() {
 var theModal = $(this).data("target"),
 videoSRC = $(this).attr("data-video"),
 videoSRCauto =
 videoSRC +
 "?modestbranding=1&rel=0&controls=0&showinfo=0&html5=1&autoplay=1";
 $(theModal + " video").attr("src", videoSRCauto);
 });
 $("#videoModal").on("hide.bs.modal", function() {
 $("video").attr("src", "");
 });
});
function video(vid) {
 $(vid).attr("src", $(vid).attr("src"));
}
$("#modal1").on("hidden.bs.modal", function() {
 video("#modal1 video");
});
$("#modal2").on("hidden.bs.modal", function() {
 video("#modal2 video");
});
$("#modal3").on("hidden.bs.modal", function() {
 video("#modal3 video");
});
$("#modal4").on("hidden.bs.modal", function() {
 video("#modal4 video");
});
$("#modal5").on("hidden.bs.modal", function() {
 video("#modal5 video");
});
$("#modal6").on("hidden.bs.modal", function() {
 video("#modal6 video");
});
$("#modal7").on("hidden.bs.modal", function(e) {
 video("#modal7 video");
});
asked Feb 16, 2019 at 7:40
1
  • Is "hidden.bs.modal" an oddly named custom event, or did you forget an event name in event delegation, like .on('click', 'hidden.bs.modal', ...? Commented Feb 16, 2019 at 7:46

1 Answer 1

3

Use classes instead of IDs, and then you can select the video descendant dynamically, and pass it to video. For example, using a class name of .modal instead of #modal1, #modal2, ...:

function video($vid) {
 $vid.attr("src", $vid.attr("src"));
}
$(".modal").on("hidden.bs.modal", function() {
 video($(this).find('video'));
});
answered Feb 16, 2019 at 7:44

4 Comments

wow, can't believe how quickly you answered that. I've been trying to work it out for over an hour now! Makes perfect sense, should have looked to target a class and use $this. Thanks!
I would (personally, at least) argue that using data- attributes as JS/jQuery selectors and leaving classes for CSS only is better practice.
@Jeto That's the first time I've heard that, sounds a bit odd but not that odd, do you have a link or something I can look at to read an argument for it?
@CertainPerformance css-tricks.com/stop-using-css-selectors-non-css for instance. There's several arguments, but the main one is to keep styling and scripting as separate as possible. Also justmarkup.com/log/2017/07/… (which is linked from the previous one).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.