I am trying to add a javascript alert to my form, the problem I am having is that when the user clicks cancel, the form still gets submitted?.
In the head I have :
<head>
<script type="text/javascript">
function confirmDelete() {if(confirm('Are you sure you want to delete this category ?'))
alert('Category Deleted !');
else alert('Cancelled !')}
</script>
</head>
My link uses the following :
a onClick="confirmDelete()"
Im not sure why clicking cancel still submits it?, is it possible to show Cancelled ! for 2 seconds and then close also ?.
I am trying to add a javascript alert to my form, the problem I am having is that when the user clicks cancel, the form still gets submitted?.
In the head I have :
<head>
<script type="text/javascript">
function confirmDelete() {if(confirm('Are you sure you want to delete this category ?'))
alert('Category Deleted !');
else alert('Cancelled !')}
</script>
</head>
My link uses the following :
a onClick="confirmDelete()"
Im not sure why clicking cancel still submits it?, is it possible to show Cancelled ! for 2 seconds and then close also ?.
Share Improve this question asked Nov 26, 2012 at 11:13 Iain SimpsonIain Simpson 8,14113 gold badges49 silver badges67 bronze badges 1- 1 This should help you: stackoverflow./questions/932653/… – pabrantes Commented Nov 26, 2012 at 11:15
3 Answers
Reset to default 4try using the cancel
function confirmDelete(e) {
if(confirm('Are you sure you want to delete this category ?'))
alert('Category Deleted !');
else {
alert('Cancelled !');
e.preventDefault();
}
}
and change your onclick to
onclick="confirmDelete(event)"
You need to return the result from the confirm:
function confirmDelete() {
return confirm('Are you sure you want to delete this category ?');
}
onClick="confirmDelete()"
you can use jQuery click event and inside event put e.preventDefault();
and it will prevent default and default is submiting form-