Orb Hitter
Orb Hitter

Reputation: 317

How do I stop audio from continuing to play after removing video tag with jQuery?

The audio keeps playing when I use the remove function to remove the video. This doesn't happen if I don't include autoplay as an attribute in the video tag. I tried different things and nothing seems to work.

//$("#video").pause();
//$("#video").stop();
$("#video").empty();
$("#video").remove();

Upvotes: 7

Views: 6986

Answers (3)

Cristian Triviño
Cristian Triviño

Reputation: 21

firsly try to create "video" tag empty in the html and create "source" tag into javascript code

<html>
.
.
<video id="main-video" autoplay=""></video>
.
.
</html>


<script>
   $('#main-video').append('<source type="video/mp4" src="URL.mp4">');
</script>

Upvotes: 1

user1357678
user1357678

Reputation:

The pause function that you're trying to call (first commented out line) exists on the DOM object, not the jQuery object.

You can get the DOM object through jQuery's get function, example: $('element').get()[0].

http://jsfiddle.net/calvintennant/bTmHn/

Upvotes: 0

John Do
John Do

Reputation: 66

Might be a little late, but I had the same issue and the above answer didn't worked for me (yes the tag was created by jQuery)

I got it working by using

jQuery('#myvideoTag').trigger('pause');

Upvotes: 5

Related Questions