im making item cards with ojct from sql and with foreach i create cards. ive made when i press a card it selects itself with a red border and the buy button changes to edit ant delete. but it only works with the first card the other cards dont work and i cant find reason.
<?php $i = 0; ?>
<div class="container marketing">
<?php foreach ($devilRooms as $devilRoom) { ?>
<?php $i++; ?>
<div class="card optionsecoptions" id="card" style="width: 18rem;">
<?php
echo '<img class="bd-placeholder-img bd-placeholder-img-lg feature-image img-fluid mx-auto" width="500" height="500" src="img/nr' . $i . '.png" alt="">';
?>
<div class="card-body">
<h5 class="card-title">
<?= $devilRoom->brand ?>
</h5>
<h5 class="card-title">
<?= $devilRoom->item ?>
</h5>
<hr>
<p>
<?= $devilRoom->descript ?>
</p>
<hr>
<p class="price">
<?= $devilRoom->price ?>$</p>
</div>
<div class="card-body">
<form class="button" id="edit" action="" method="post">
<input type="hidden" name="id" value="<?= $devilRoom->id ?>">
<button type="submit" name="edit" class="btn btn-primary">edit</button>
</form>
<br>
<form class="button" id="destroy" action="" method="post">
<input type="hidden" name="id" value="<?= $devilRoom->id ?>">
<button type="submit" name="destroy" class="btn btn-danger">delete</button>
</form>
<form class="button" id="buy" action="" method="post">
<input type="hidden" name="id" value="<?= $devilRoom->id ?>">
<button type="submit" name="buy" class="btn btn-success">buy</button>
</form>
</div>
</div>
<?php } ?>
</div>
<script>
document.body.addEventListener('click', function(event) {
var closestOption = findClosestByClassName(event.target, 'optionsecoptions');
if (closestOption) {
var selectedEl = document.querySelector(".selected");
if (selectedEl && selectedEl !== closestOption) {
selectedEl.classList.remove("selected");
}
closestOption.classList.toggle("selected");
}
});
const btn = document.getElementById('card');
btn.addEventListener('click', () => {
const edit = document.getElementById('edit');
const destroy = document.getElementById('destroy');
const buy = document.getElementById('buy');
if (edit.style.display === 'none' && destroy.style.display === 'none' && buy.style.display === 'block') {
edit.style.display = 'block';
destroy.style.display = 'block';
buy.style.display = 'none';
} else {
edit.style.display = 'none';
destroy.style.display = 'none';
buy.style.display = 'block';
}
});
function findClosestByClassName(el, cls) {
while (!el.classList.contains(cls)) {
el = el.parentNode;
if (!el) {
return null;
}
}
return el;
}
</script>
ive added a video how it works
all of them should work the same as the first one