.pause()
and reset currentTime
on modal close.src
attribute to stop playback.You're trying to stop a video from playing after closing a modal, but your current code ($('#modal-video')[0].pause();
) likely targets the element incorrectly or doesn't account for iframe-based embeds like YouTube.
<video>
element, the pause()
method can work..pause()
—you’ll need to reset the iframe src or use the appropriate API.<video>
ElementEnsure the element has the correct ID or class and is a valid <video>
tag.
Use this code:
Example jQuery: $('#modal-video').get(0).pause();
To ensure it resets as well:
```js
var video = $('#modal-video').get(0);
video.pause();
video.currentTime = 0;
```
The pause()
method does not work on iframe embeds directly.
A simple workaround: remove and re-add the iframe on modal close to stop playback.
Modify the iframe's src
attribute like this:
```js
var iframe = $('#modal-video');
var src = iframe.attr('src');
iframe.attr('src', '');
iframe.attr('src', src);
```
Or, only remove the src
on close, and restore when the modal opens.
display: none
change.To stop a video after closing a modal:
.pause()
and currentTime = 0
.src
attribute.Let me know if you're using a specific video provider—YouTube, Vimeo, or HTML5—so I can refine this further.