I get Comments for a post (on a blog I'm coding) with a foreach loop.
If an admin is logged in they get an 'x'-Button next to every comment to delete them.
When the button is clicked it should open a Modal in which the admin has to confirm he wants to delete the comment.
The Problem is: Right now only the first Button opens the Modal and I don't know why since I know exactly nothing when it comes to javascript, I did my script from a little tutorial about modal boxes.
This is the code of the site itself and the script of it:
<?php if(isset($_SESSION['admin']) && $_SESSION['admin'] == true): ?>
<button id="delete-btn" class="delete-button">x</button>
<?php endif;?>
<div id="delete-modal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p class="modal-text"> Are you sure you want to delete this comment?
<br /><br /> You will not be able to undo this.</p>
<form method="post" action="post?id=<?php echo e($post['id']);?>">
<div class="confirmation-delete">
<a href="delete-comment?id=<?php echo e($comment->id); ?>" name="delete-comment">delete comment<a/>
</div>
</form>
</div>
</div>
<script>
var modal = document.getElementById('delete-modal');
var btn = document.getElementById("delete-btn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
When I click on the button next to the first comment the modal opens, but if I click the button next to the following comments it doesn't.
I don't know what's wrong or how to get it working, I hope anyone can help me with this.