I am making a sort of mini gallery for myself as a personal project. I am displaying mini pictures and if i click on them, a modal should pop up displaying entire picture.
<dialog>
<img src="static/img/img (1).jpg" height="600">
<button autofocus>Close</button>
</dialog>
<button class="btn">
<img src="static/img/img (1).jpg" height="200" width="200">
</button>
<dialog>
<img src="static/img/img (2).jpg" height="600">
<button autofocus>Close</button>
</dialog>
<button class="btn">
<img src="static/img/img (2).jpg" height="200" width="200">
</button>
this is my html structure. There are many more pics, this is just the first two.
const dialog = document.querySelector("dialog");
const showButton = document.querySelector("dialog + button");
const closeButton = document.querySelector("dialog button");
// "Show the dialog" button opens the dialog modally
showButton.addEventListener("click", () => {
dialog.showModal();
});
// "Close" button closes the dialog
closeButton.addEventListener("click", () => {
dialog.close();
});
This is my js file.
Problem is first modal is working fine, but none of the other modal is working. How can I make it work?
2
Answers
Using
querySelectorAll
=>This should do the job