I have created banks displayed from the database in the form of cards.
How can I take the id of the document I click on and delete this card from the database?
function getBanks() {
db.collection("banks").onSnapshot((snapshot) => {
let banks = [];
snapshot.docChanges().forEach((change) => {
const bank = change.doc.data();
if (change.type === "added") {
banks.push(bank);
generateBanks([bank]);
} else if (change.type === "removed") {
console.log("Removed bank: ", bank );
}
});
});
}
function generateBanks(banks) {
banks.forEach((bank) => {
...
const bank_delete_el = document.createElement("button");
bank_delete_el.classList.add("delete");
bank_delete_el.innerText = "Delete";
});
});
}
2
Answers
Here is a simple example using click event listener and firebase
delete
:Here are the docs for
addEventListener
and firebase delete documents.You can try adding the
id
of the document as adata-attribute
, eg:This will create the button as
Then when you click the delete button you can get the id and delete it from firestore