Last Updated: February 25, 2016
·
6.829K
· carlosagile

How to pause a video/audio when modal windows is closed

  • Example of how to pause a video or audio contained in a bootstrap-modal to prevent further hearing when the modal window is closed.
// Close Bootstrap Modal.
$('.close, .save, .cancel').on('click', function(e){
    e.preventDefault();
    var $this = $(this);
    // get identifier modal window.
    var modal = $this.data('custom');

   $('#'+modal).modal('hide');
  // check tag html5 for video and audio.
   if (($('#id-'+modal).prop("tagName") == 'AUDIO') 
   || ($('#id-'+modal).prop("tagName") == 'VIDEO')) {
       if (!$('#el-'+modal).get(0).paused) {
              $('#el-'+modal)[0].pause();
       }
   }
});