Reputation: 81
I'm using Django in my project and I'm getting some problems with the confirm
function in order to prevent item deletion from datatable option.
Despite cancelling in the confirm pop-up, the link still sends me to the default link href and the object is deleted.
This is my HTML:
<a class='btn btn-danger btn-xs' id="elimina-objeto" onclick="confirmaEliminacion()" href="{% url 'eliminarQueja' queja.id %}">Eliminar</a>
And my JS:
function confirmaEliminacion() {
var res = confirm("Va a eliminar el objeto seleccionado. Si desea continuar, pulse aceptar.");
if (res) {
return false;
} else {
document.getElementById("elimina-objeto").href = "#";
}
}
Upvotes: 0
Views: 494
Reputation: 5303
Make a small change to your HTML:
<a class='btn btn-danger btn-xs' id="elimina-objeto" onclick="return confirmaEliminacion()" href="{% url 'eliminarQueja' queja.id %}">Eliminar</a>
Also, you should be returning true
from your javascript function if the user wants to delete and follow the original href, and false
if they do not want to delete. Changing the href value itself is not necessary:
function confirmaEliminacion() {
var res = confirm("Va a eliminar el objeto seleccionado. Si desea continuar, pulse aceptar.");
if (res) {
return true;
} else {
return false;
}
}
Upvotes: 3