Hello! I’m working on a video gallery with modals, and I’ve found a solution to open the modal and enable autoplay with audio using the following code here.
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function () {
modal.style.display = "block"
document.getElementById('myVideo').play(); // <-- PLAY
}
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
document.getElementById('myVideo').pause(); // <-- PAUSE
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
document.getElementById('myVideo').pause(); // <-- PAUSE
}
}
</script>
I have 3 modals, but currently, it only works with one of them. The project is local and should be in HTML5, without using CMS.
<script>
// Get the modal
var modal = document.getElementById("myModal");
var modal = document.getElementById("myModal-2");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
var btn = document.getElementById("myBtn-2");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function () {
modal.style.display = "block"
document.getElementById('myVideo').play(); // <-- PLAY
document.getElementById('myVideo-2').play(); // <-- PLAY
}
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
document.getElementById('myVideo').pause(); // <-- PAUSE
document.getElementById('myVideo-2').pause(); // <-- PAUSE
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
document.getElementById('myVideo').pause(); // <-- PAUSE
document.getElementById('myVideo-2').pause(); // <-- PAUSE
}
}
</script>
The reason that it works only on one is that you do not grab all “modals” and it returns only the last one. Another issue is that you are getting modals by id but in the first example you are getting only one in the second example you are getting two but it would not work either and it is not DRY anyway.
You need to grab them all(eg. with class) and on each apply your events. Something like that:
const modals = Array.from(document.querySelectorAll(".myModal"));
modals.forEach(modal => {
// do your stuff for modal like grab element inside an add event listener etc.
}
This is a false statement in your code you are giving instructions if target IS modal and not if target is NOT modal.
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
// Do stuff
}
}
You are missing ! before the weak equality check. So your equality check should look like this if event.target !== modal than hide modal