Append the dialog element to the body using document.body.appendChild(myDialog)
function Popup() {
var myDialog = document.createElement("dialog");
document.body.appendChild(myDialog)
var text = document.createTextNode("This is a dialog window");
myDialog.appendChild(text);
myDialog.showModal();
}
<button onclick="Popup()" role='button'>
Show Alert
</button>
Answer from brk on Stack Overflowhtml - Is it possible to create modal popup only from javascript - Stack Overflow
How to make modal popup after button pressed
Creating a modal using CSS, using buttons to trigger the modal popup
html - how to write a modal popup in javascript - Stack Overflow
Videos
Append the dialog element to the body using document.body.appendChild(myDialog)
function Popup() {
var myDialog = document.createElement("dialog");
document.body.appendChild(myDialog)
var text = document.createTextNode("This is a dialog window");
myDialog.appendChild(text);
myDialog.showModal();
}
<button onclick="Popup()" role='button'>
Show Alert
</button>
No there is no "dialog" element that has that functionality. You'll have to create a regular element append it to the document and then have css to position it.
You are actually describing the native bootstrap's modal's behavior. I'm not sure I'm understanding your problem well but:
Given a modal with the id #modalSignin, you should implement the signup button like so:
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalSignin"> ... </button>
OR if for whatever reason you can't use the data-bs-target attribute, you should then use a specific event listener:
$('#your-signup-button-id').on('click', function() {
var modalToggle = new bootstrap.Modal(document.getElementById('the-modal-id'))
modalToggle.show()
});
Alternative using vanilla JS:
document.getElementById('your-signup-button-id').addEventListener('click', function() {
var modalToggle = new bootstrap.Modal(document.getElementById('the-modal-id'))
modalToggle.show()
});
This should do it as long as you don't have any css rule that could break the native behavior (ie. if the user can scroll on the main content for example)
See more here: Bootstrap 5 modal
Try this code
<script>
var modal = document.getElementById('modalSignin');
var btn = document.getElementById("signupButton");
btn.onclick = function() {
modal.style.display = "block";
document.body.style.overflow = "hidden"; // Prevent scrolling on the background
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
document.body.style.overflow = ""; // Enable scrolling on the background
}
}
</script>