TechQA.

Closing modal with e.keypress

1.1k views Asked by Matt At 2014-11-17T21:51:30+00:00 17 November 2014 at 21:51 2025-12-20T17:30:54+00:00

I have the following modal box:

<div class="modal-video" id="v-5417">
<div class="video-player">
                <video id="v-5417-tape" preload="none">
                    <source type="video/mp4" src="videos/anthem-od47.mp4">
                    <source type="video/webm" src="videos/anthem-od47.webm">
                </video>
                <div class="close-modal fade-control"></div>
</div>
</div>

and trying to use the following e.keyCode to close the modal:

        $(document).keydown(function (e) {
            if (e.keyCode == 27) {
                $(".modal-video").hide();
            }
        });

This is only hiding the video, but not closing the modal and killing the video. How can I completely close the modal and video together?

javascript jquery modal-dialog keycode
Original Q&A
2

There are 2 answers

0
Marian Gibala Marian Gibala On 2014-11-17T22:32:21+00:00 17 November 2014 at 22:32

jQuery .hide() just changes the CSS style to display:none, so your video is hided and still playing in the background. To fix that issue, you can stop playing the video by standard HTML5 pause() method.

If hiding $(".modal-video") doesn't work for you, then I assume you need to hide it's parent - but we have to see more your code to be sure about that.

$(document).keydown(function (e) {

    if (e.keyCode == 27) {

        var video = document.getElementById("v-5417-tape");
        video.pause()

        $(".modal-video").parent().hide();


    }
});

If you are using some kind of player template or plugin, and click on class "close-modal" hides the modal as you expect - then you can use jQuery .toggle() method to call click event on that element, when you press your key.

$(document).keydown(function (e) {

    if (e.keyCode == 27) {

        var video = document.getElementById("v-5417-tape");
        video.pause()

        $(".close-modal").toggle("click")


    }
});
0
Matt Matt On 2014-11-18T18:48:33+00:00 18 November 2014 at 18:48

The active state has to be removed, and that disabled the popup.

$(document).keydown(function (e) {
        var video = document.getElementById("v-5417-tape");
        if (e.keyCode == 27) { 
        if(video.play){
            video.pause();
        }
            $(".modal-video").removeClass("active");

        }
    });

Related Questions in JAVASCRIPT

  • Using Puppeteer to scrape a public API only when the data changes
  • inline SVG text (js)
  • An array of images and a for loop display the buttons. How to assign each button to open its own block by name?
  • Storing the preferred font-size in localStorage
  • Simple movie API request not showing up in the console log
  • Authenticate Flask rest API
  • Deploying sveltekit app with gunjs on vercel throws cannot find module './lib/text-encoding'
  • How to request administrator rights?
  • mp4 embedded videos within github pages website not loading
  • Scrimba tutorial was working, suddenly stopped even trying the default
  • In Datatables, start value resets to 0, when column sorting
  • How do I link two models in mongoose?
  • parameter values only being sent to certain columns in google sheet?
  • Run main several times of wasm in browser
  • Variable inside a Variable, not updating

Related Questions in JQUERY

  • In Datatables, start value resets to 0, when column sorting
  • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
  • window.location.href redirects but is causing problems on the webpage
  • Using JQuery Date Slider
  • Storing selected language in localStorage
  • How to stop other divs from still showing when i click a different button?
  • Check multiple values with jQuery
  • Bootstrap component does not want to render in Datatables function
  • put white spaces when entering an amount moneytype symfony
  • Trouble accessing custom header in AJAX response using jQuery in Fiware Keyrock
  • I just cant make it work, HTML, JS and Firebase error
  • Didn't declared variable still not getting any error in JavaScript
  • Move element horizontally while scrolling vertically in pure JavaScript
  • allow multi carousel in same page
  • Embedded TikTok posts / thumbnail styling issue

Related Questions in MODAL-DIALOG

  • Modals in React applications
  • Modal won't hide
  • How to get the form data from the modal-component into the submithandler
  • Modal exits data iteration loop in Statamic
  • Where to put and how to use Modal
  • JavaScript is not communicating with my HTML file
  • Create custom dialog box swiping on all 4 directions
  • modal is not shown up when convert to vue 3
  • How can I modify my Chrome extension to display a custom confirmation modal before a file is uploaded?
  • angular test inside subscribe of modal
  • Unable to close modal: setter function used to change variable state which closes modal never executes
  • How can you interact with a modal from apps script after it has already run?
  • Bootstrap modal in php loop
  • Link a Modal to Other Modal in ASP.NET
  • Livewire/Bootstrap - Issue Passing Value to Input Before Displaying Livewire/Bootstrap Modal

Related Questions in KEYCODE

  • working on serial/references number input filed - validations-keycodes
  • combine KeyCode pressed and modifiers to get final KeyCode
  • why do I have to press escape on the keyboard twice in vb.net
  • Peculiarities of grabbing keys and scancodes different methods LINUX
  • How to use keycodes in an array to filter which keys are taken as input within a 'keyup' event listener?
  • How to get key presses / combinations without being modified into alt combinations?
  • Ubuntu GDK C++ how to get the code of a key using CAPSLOCK
  • key code 10182 for Exit button in tizen studio?
  • I need to give condition between english keyboard layout and japanese keyboard layout using jquery
  • The part keeps rotating even when l press space bar
  • why am i getting null value eve if i press 'asdfghjkl'?
  • Convert KeyEvent DPad
  • KeyDown event doesnt trigger with Shift. Other Keys Work
  • Problem with Window.event.keyCode and .dat memory read VBS hta
  • Ubuntu, C++, xkb/symbols files: Is there a mapping between keynames and keycode?

Popular Questions

  • How do I undo the most recent local commits in Git?
  • How can I remove a specific item from an array in JavaScript?
  • How do I delete a Git branch locally and remotely?
  • Find all files containing a specific text (string) on Linux?
  • How do I revert a Git repository to a previous commit?
  • How do I create an HTML button that acts like a link?
  • How do I check out a remote Git branch?
  • How do I force "git pull" to overwrite local files?
  • How do I list all files of a directory?
  • How to check whether a string contains a substring in JavaScript?
  • How do I redirect to another webpage?
  • How can I iterate over rows in a Pandas DataFrame?
  • How do I convert a String to an int in Java?
  • Does Python have a string 'contains' substring method?
  • How do I check if a string contains a specific word?

Trending Questions

  • UIImageView Frame Doesn't Reflect Constraints
  • Is it possible to use adb commands to click on a view by finding its ID?
  • How to create a new web character symbol recognizable by html/javascript?
  • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
  • Heap Gives Page Fault
  • Connect ffmpeg to Visual Studio 2008
  • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
  • How to avoid default initialization of objects in std::vector?
  • second argument of the command line arguments in a format other than char** argv or char* argv[]
  • How to improve efficiency of algorithm which generates next lexicographic permutation?
  • Navigating to the another actvity app getting crash in android
  • How to read the particular message format in android and store in sqlite database?
  • Resetting inventory status after order is cancelled
  • Efficiently compute powers of X in SSE/AVX
  • Insert into an external database using ajax and php : POST 500 (Internal Server Error)
  • Privacy
  • Terms
  • Cookies
  • Homegardensmart
  • Math
  • Aftereffectstemplates