Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
91
rated 0 times [  97] [ 6]  / answers: 1 / hits: 21874  / 5 Years ago, fri, february 1, 2019, 12:00:00

I'm looking for a way to dynamically display a video in a modal pop-up, without being embedded to YouTube.





$(function() {
$(.video).click(function () {
var theModal = $(this).data(target),
videoSRC = $(this).attr(data-video),
videoSRCauto = videoSRC + ;
$(theModal + ' source').attr('src', videoSRCauto);
$(theModal + ' button.close').click(function () {
$(theModal + ' source').attr('src', videoSRC);
});
});
});

<head>
<link rel=stylesheet href=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css>
<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js></script>
<script src=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js></script>
</head>

<button class=btn btn-lg video data-video=https://clienti.dk/media/1140/friheden-video.mp4 data-toggle=modal data-target=#videoModal>Play Video</button>

<div class=modal fade id=videoModal tabindex=-1 role=dialog aria-labelledby=myModalLabel aria-hidden=true>
<div class=modal-dialog>
<div class=modal-content>
<div class=modal-body>
<button type=button class=close data-dismiss=modal aria-label=Close><span aria-hidden=true>&times;</span></button>
<video controls>
<source src= type=video/mp4>
</video>
</div>
</div>
</div>
</div>





YouTube Iframe
- Working example with embedded youtube video in iframe



Regular video
- Here I tried to use html video instead. The video gets the right src but it might be a problem with load timing.



Can anyone help me out?


More From » jquery

 Answers
17

Everything looks good, you are just missing $(theModal + ' video').load();. You have to load the external video if you are setting src runtime.



See the Snippet below:





$(function() {
$(.video).click(function () {
var theModal = $(this).data(target),
videoSRC = $(this).attr(data-video),
videoSRCauto = videoSRC + ;
$(theModal + ' source').attr('src', videoSRCauto);
$(theModal + ' video').load();
$(theModal + ' button.close').click(function () {
$(theModal + ' source').attr('src', videoSRC);
});
});
});

<head>
<link rel=stylesheet href=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css>
<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js></script>
<script src=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js></script>
</head>

<button class=btn btn-lg video data-video=https://clienti.dk/media/1140/friheden-video.mp4 data-toggle=modal data-target=#videoModal>Play Video</button>

<div class=modal fade id=videoModal tabindex=-1 role=dialog aria-labelledby=myModalLabel aria-hidden=true>
<div class=modal-dialog>
<div class=modal-content>
<div class=modal-body>
<button type=button class=close data-dismiss=modal aria-label=Close><span aria-hidden=true>&times;</span></button>
<video controls width=100%>
<source src= type=video/mp4>
</video>
</div>
</div>
</div>
</div>





You can test it here


[#52671] Monday, January 28, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isham

Total Points: 69
Total Questions: 86
Total Answers: 86

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;