/    Sign up×
Community /Pin to ProfileBookmark

Closing a modal

Hi,
I have a modal with a video. And the modal is a container with a video in it. I want users to be able to close the modal when they click outside the video.
I took the container and added an event listener to it.
The problem is that the modal gets closed even if they click on the video. With is something I don’t want. I want them to be able to click on the video to pause play etc. It also created a weird interaction if the video is the full screen as if you click on the video to pause it. The modal gets close and the video stays full screen and it’s bugged out.

here is the function

“`
const playVideo = () => {
const videoContainer = document.querySelector(“.video-container”);
const video = document.querySelector(“.video-container video”);
const playButton = document.querySelector(“.main-button img”);
const body = document.querySelector(“body”);

playButton.addEventListener(“click”, () => {
videoContainer.classList.add(“show-video”);
video.play();
body.style.overflow = “hidden”;
});

videoContainer.addEventListener(“click”, () => {
videoContainer.classList.remove(“show-video”);
video.pause();
body.style.overflow = “initial”;
});
};
playVideo();
“`

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinAug 18.2021 — maybe something like this

sos you know whats clicked on

``<i>
</i>&lt;style&gt;
*{border-style: solid;}
&lt;/style&gt;
&lt;div style="width:8em;background:silver;"&gt;
div content
&lt;p&gt;
paragraph
&lt;span&gt;
span
&lt;/span&gt;
more paragraph
&lt;/p&gt;
more div content
&lt;/div&gt;
&lt;script&gt;
'use strict';
onclick=function(e){alert(e.target)}
&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumAug 18.2021 — In `event.target</C> you have access to the element that has been clicked originally. You can check if the video container has been clicked and in this case close the modal only:
<CODE>
`<i>
</i> videoContainer.addEventListener("click", (event) =&gt; {
if (event.target == videoContainer) {
videoContainer.classList.remove("show-video");
video.pause();
body.style.overflow = "initial";
} else {
// the element having been click is not the video container
// it is the video or something else
}
});<i>
</i>
``
Copy linkTweet thisAlerts:
@codyhillauthorAug 18.2021 — @Sempervivum#1635658 Thank you!
×

Success!

Help @codyhill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...