``Do not forget to close the modal window by clicking outside of it or

I am looking for a way to close the modal window either when a user clicks outside of it or presses the escape key on the keyboard.

Despite searching through numerous posts on SO regarding this issue, I have been unable to find a solution that works with my specific code.

http://jsfiddle.net/Draven/yCTA3/100/

HTML:

<div class="audio-lg">
    <a href="#openAudio" ref="openAudio">
        <img src="http://s30.postimg.org/9nq5lwwr5/audio_default_thumbnail.jpg" class="poster" alt="Test Title" width="320px" height="180px">
        <span class="play-static"></span>
    </a>
</div>
<div id="openAudio" class="show_player">
    <div class="pp_pic_holder" style="display:block">
        <div class="ppt" style="opacity:1;display:block;width:500px;height:20px"></div>
        <div class="pp_content_container">
            <div class="pp_content" style="min-height:248px;width:500px">
                <div class="pp_fade" style="display:block">
                    <div id="pp_full_res" style="background-color:black;text-align:center">
                        <audio class="player" id="player" preload="auto" controls>
                            <source src="http://www.w3schools.com/html/horse.mp3" type="audio/mpeg">
                            Your browser does not support the audio element.
                        </audio>
                    </div>
                    <div class="pp_details" style="width:500px">
                        <p class="pp_description" style="display:block">Test Title</p>
                        <a class="pp_close" href="#" ref="closeAudio" style="color:#fff">Close</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="overlay"></div>
</div>

CSS:

.show_player {
    opacity: 0;
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 530px;
    visibility: visible;
    z-index: 10;
}
.show_player:target {
    opacity: 1;
    display: block;
    top: 0;
    visibility: visible;
}
.show_player .overlay {
    background: rgba(68,68,68,0.7);
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

JQuery:

$('a[ref=openAudio]').click(function(){
    $('#player').get(0).play();
});
$('a[ref=closeAudio]').click(function(){
    $('#player').get(0).pause();
});

Answer №1

Another excellent solution with the added ESC functionality.

UPDATED

$(document).keyup(hideModal);
$(".overlay").click(hideModal);


function hideModal(e) {
    if (e.keyCode === 27 || e.type === 'click') {
        window.location.hash = "#";
    }

    $('#player').get(0).pause();
}

JSFIDDLE

Answer №2

Take a look at this: http://jsfiddle.net/yCTA3/113/

I have removed the pointer-events:none attribute from the overlay class.

Additionally, I have added a click event on the overlay.

$(".overlay").click(function(){
   window.location.hash = "#";
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Design an element that stretches across two navigation bars

Currently, I have implemented two Navbars on my website as shown in the image below: https://i.stack.imgur.com/4QmyW.png I am now looking to include a banner that clearly indicates that this site is a test site. In addition, I would like to incorporate a ...

When I click the button, it deletes the DOM element and hides it, preventing me from

I'm facing a simple issue that I can't quite wrap my head around. Whenever I input a value into the form and click the button to run the function, the 'loading' element disappears and doesn't reappear. Here is the JavaScript code ...

"Every time ajax is called, it will always generate

function lks() { var groupname = document.getElementById('groupname').value; $.ajax ({ url: 'verifyGroup.php?groupname='+groupname, type: 'get', ...

Performing an Ajax post request to a PHP script in order to retrieve a PHP variable using XMLHttpRequest

I am looking to dynamically update my table using JavaScript every few seconds. Currently, I have set up an AJAX post request to my update.php file and trigger it if it is set. Then, I execute a MySQL query to retrieve the data and store the resultset in ...

Unable to activate the on('click') event when the button is loaded via AJAX

I am facing an issue with the on('click') event. I have a button that is loaded dynamically via ajax and has a click event attached to it. However, when I try clicking it, nothing happens even though the expected output should be showing an alert ...

An error occurred while the server was processing the request during an Ajax POST operation

My jQuery Mobile app communicates with a WCF REST service, and most of the calls to the service are GETs. However, one particular function uses a POST request with JSON data. While the POST request works perfectly on our development servers, it fails to w ...

Switch up the background image of the <li> element when the <a> tag is clicked

html: <ul> <li id="Co" class="libgc" ><b>CO</b></li> <li id="NSCO" class="libgc active"><span> <a href="#NSale.php" target="homeFrame" class="aclass">NSale</a></span></li> </ul ...

What could be causing the issue of my p tag not being centered within both of my content divs?

I'm struggling to center the paragraphs within the divs with classes .content1 and .content2. Can anyone help me figure out why? Check out the demo here Here is the code: .content1 p, .content2 p { text-align: center; background-color: rgba ...

Any tips or hacks for successfully implementing vw resizing on the :before pseudo-element in webkit browsers?

When using vw sizes on webkit browsers, a well-known bug can occur where they do not update when the window is resized. The typical workaround for this issue has been to employ javascript to force a redraw of the element by reassigning the z-index upon res ...

Is it possible to manipulate CSS on a webpage using javascript?

I have incorporated this piece of JavaScript to integrate a chat box onto my website: window.HFCHAT_CONFIG = { EMBED_TOKEN: "XXXXX", ACCESS_TOKEN: "XXXXX", HOST_URL: "https://happyfoxchat.com", ASSETS_URL: "https://XXXXX.cloudfront.ne ...

Implementing auto-population of input field in Vue JS based on dropdown selection

I'm in search of a solution for automatically filling input fields in Vue.js. My form consists of various input types such as text, select dropdowns, and quantities. I want the vCPU, vRAM, and Storage Capacity fields to be filled with predefined value ...

Add a jQuery click function within a loop indefinitely

Hello there, I have a simple loop set up in PHP and I am trying to figure out how to create an event that triggers whenever the user clicks on any line generated by this loop. Here's my basic PHP while loop: <?php $x = 1; while($x <= 5) { ...

Every time I attempt to execute this piece of code in "node.js", an error pops up

const express = require('express'); const request = require('request'); const bodyParser = require('body-parser'); const https = require('https'); const app = express(); app.use(express.static('public')); ...

Troubleshooting AngularJS POST Request Error with Request Body

I am a beginner in AngularJs and I am trying to make a post request to a server with enum form. Currently, I have the following JavaScript code: function completeTaskAction2($scope, $http, Base64) { $http.defaults.headers.common['Authorization'] ...

Enabling client-side access to collections without the need for meteor startup

Whenever I define my Meteor collections on the server and attempt to access them in the client without being within any of the predefined Meteor methods such as rendered, events, created, or helpers, I consistently encounter an error stating Meteor colle ...

Numeric keypad causing issues with setting minimum and maximum lengths and displaying submit button

I designed a password pin page that resembles a POS NUMPAD for users to enter their password. I am struggling to make the condition specified in the JavaScript function properly. Rule: Hide the submit button if the minimum input is less than 4 characters ...

Developing a custom function to retrieve an array as a callback

I'm currently diving into the world of Node.js Struggling with implementing my own callback function in a certain method. It may seem like a straightforward task, but I'm finding it quite challenging to grasp. This specific function takes an ad ...

Strange jQuery IE7 Modal Behavior

Recently, I encountered a frustrating issue with my modal windows. They seem to work perfectly in all browsers except for IE7 (I have not tested below this version). In the demo I provided, you will see that the first modal-dialog functions as expected. H ...

JQuery allows for multiple drag and drop functionalities, including events that occur after an object

I'm currently working on a .php page where I need to implement a drag-and-drop functionality. There will be 3 draggable elements that must be placed in their corresponding droppable elements - each draggable can only be dropped in a specific droppable ...

Repetitive Anchor IDs and Web Standards Compliance

Today, I encountered the task of converting outdated attributes on a page to make it more XHTML friendly. This led me to consider the anchor tag and its usage: Initially, I had anchor tags in the form of <a name="someName"></a>. However, upon ...