I've got a two Modal windows, one is for adding data, the other for editing. The following 'Modal' does work, it closes when span (x) is pressed, or any other place is clicked.
<span class="close">×</span>
<form id="modal-form" method="POST" action="action.php">
<button id="form-submit" type="submit"></button>
</form>
However, this 'Modal2' does not react to close button. How is that possible? THey are in one html page, and javascript modal.js is included in <body>
tag.
<span class="close">×</span>
<form id="modal-form" method="POST" action="action.php">
<input type="hidden" id= "idbs" name="idbs" />
<button id="form-submit" type="submit"></button>
</form>
and the Javascript for span is:
// Get the modal
var modal = document.getElementById('Modal');
// Get the button that opens the modal
var openBtn = document.getElementById("openModal");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
Hope the below solution works for you. Basically, I pass Modal ID in buttons as
data
attributes.Note: I assume that the modal markup structure remains the same, otherwise the code
element.parentNode.parentNode...
will not work.On click of the button, the modal dialog is displayed (using data attribute) and on click of
span
element in the modal, I get handle to parentdiv
(having Modal ID) and close it.Try this: