I am trying to retrieve the id of a selected button for deletion purposes. However, I am unable to get the id from it using 'this.id'. Is there another method I should be using?
Below is the code where I create the button:
var deleteEmployer= document.createElement("td");
var deleteEmployerButton= document.createElement("input");
deleteEmployerButton.setAttribute("type", "submit");
deleteEmployerButton.setAttribute("value", "delete");
deleteEmployerButton.setAttribute("id", employer.employer_id);
deleteEmployer.appendChild(deleteEmployerButton);
deleteEmployer.addEventListener("click", deleteEmployFunc);
row.appendChild(deleteEmployer);
document.querySelector("#Employer").appendChild(row);
This is the function deleteEmployFunc
:
function deleteEmployFunc() {
var id = this.id;
console.log(id);
fetch("restservices/employer/" + id, { method: 'DELETE' })
.then(function (response) {
if (response.ok) {
window.location.reload();
} else if (response.status == 404) {
console.log("Employer not found")
} else {
console.log("Can't delete employer")
}
});
}