I have 3 buttons with the same class. And when i press one of them i don't want to show all the 3 forms, i want to show just the form on which button was pressed(and i can't have different classes). How can i do it?
const buttonEdit = document.querySelectorAll(".buttonEdit");
const formPost = document.querySelectorAll(".formPost");
for (var i = 0; i < buttonEdit.length; i++) {
buttonEdit[i].addEventListener("click", function(e) {
for(let form of formPost ){
form.style.display = 'block';
}
});
}
fetch(URL)
.then((response) => response.json())
.then((data) => {
data.posts.map((post) => {
const div = document.querySelector(".posts");
div.innerHTML += `
<div class="post">
<button class="buttonEdit" data-id="${post.id}">Edit</button>
</div>
<form class="formPost" method="post" action="/admin/edit" >
<label for="title">Title</label>
<input type="text" name="title">
<label for="content">Content</label>
<input type="text" name="content">
<label></label>
<input type="submit" value="Submit">
</form>
</div>
`;
});
});
Html, but if I had to do it based on what you provided, I'd either get the button which was clicked and traverse theDOMin some way to find the form which correlates to the button and show it, else I'd addidsto each button andformand get theidof the button clicked inside the event handler and use a conditional to unhide the correct form.