Simple JavaScript problem: onClick confirm not preventing default action

JavascriptHtml

Javascript Problem Overview


I'm making a simple remove link with an onClick event that brings up a confirm dialog. I want to confirm that the user wants to delete an entry. However, it seems that when Cancel is clicked in the dialog, the default action (i.e. the href link) is still taking place, so the entry still gets deleted. Not sure what I'm doing wrong here... Any input would be much appreciated.

EDIT: Actually, the way the code is now, the page doesn't even make the function call... so, no dialog comes up at all. I did have the onClick code as:

onClick="confirm('Delete entry?')"

which did bring up a dialog, but was still going to the link on Cancel.

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<script type="text/javascript">

function delete() {
	return confirm('Delete entry?')
}

</script>


...

<tr>
 <c:if test="${userIDRO}">
	<td>
	    <a href="showSkill.htm?row=<c:out value="${skill.employeeSkillId}"/>" />
		<img src="images/edit.GIF" ALT="Edit this skill." border="1"/></a>
	</td>
	<td>
	    <a href="showSkill.htm?row=<c:out value="${skill.employeeSkillId}&remove=1"/>" onClick="return delete()"/>
	    <img src="images/remove.GIF" ALT="Remove this skill." border="1"/></a>
	</td>
 </c:if>
</tr>

Javascript Solutions


Solution 1 - Javascript

There's a typo in your code (the tag a is closed too early). You can either use:

<a href="whatever" onclick="return confirm('are you sure?')"><img ...></a>

note the return (confirm): the value returned by scripts in intrinsic evens decides whether the default browser action is run or not; in case you need to run a big piece of code you can of course call another function:

<script type="text/javascript">
function confirm_delete() {
  return confirm('are you sure?');
}
</script>
...
<a href="whatever" onclick="return confirm_delete()"><img ...></a>

(note that delete is a keyword)

For completeness: modern browsers also support DOM events, allowing you to register more than one handler for the same event on each object, access the details of the event, stop the propagation and much more; see DOM Events.

Solution 2 - Javascript

Well, I used to have the same problem and the problem got solved by adding the word "return" before confirm:

onclick="return confirm('Delete entry?')"

I wish this could be heplful for you..

Good Luck!

Solution 3 - Javascript

I use this, works like a charm. No need to have any functions, just inline with your link(s)

onclick="javascript:return confirm('Are you sure you want to delete this comment?')"

Solution 4 - Javascript

I had issue alike (click on button, but after cancel clicked it still removes my object), so made this in such way, hope it helps someone in the future:

 $('.deleteObject').click(function () {
	var url = this.href;
	var confirmText = "Are you sure you want to delete this object?";
	if(confirm(confirmText)) {
		$.ajax({
			type:"POST",
			url:url,
			success:function () {
   		    // Here goes something...
            },
		});
	}
    return false;
});

Solution 5 - Javascript

Using a simple link for an action such as removing a record looks dangerous to me : what if a crawler is trying to index your pages ? It will ignore any javascript and follow every link, probably not a good thing.

You'd better use a form with method="POST".

And then you will have an event "OnSubmit" to do exactly what you want...

Solution 6 - Javascript

First of all, delete is a reserved word in javascript, I'm surprised this even executes for you (When I test it in Firefox, I get a syntax error)

Secondly, your HTML looks weird - is there a reason you're closing the opening anchor tags with /> instead of just > ?

Solution 7 - Javascript

<img src="images/delete.png" onclick="return confirm_delete('Are you sure?')"> 



<script type="text/javascript">
function confirm_delete(question) {
  
  if(confirm(question)){
  
 	 alert("Action to delete");
  
  }else{
	return false;  
  }
  
}
</script>

Solution 8 - Javascript

If you want to use small inline commands in the onclick tag you could go with something like this.

<button id="" class="delete" onclick="javascript:if(confirm('Are you sure you want to delete this entry?')){jQuery(this).parent().remove(); return false;}" type="button"> Delete </button>

Solution 9 - Javascript

try this:

OnClientClick='return (confirm("Are you sure you want to delete this comment?"));'

                            

Solution 10 - Javascript

I've had issue with IE7 and returning false before.

Check my answer here to another problem: Javascript not running on IE

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkafuchauView Question on Stackoverflow
Solution 1 - JavascriptLuca TettamantiView Answer on Stackoverflow
Solution 2 - JavascriptAraceliView Answer on Stackoverflow
Solution 3 - JavascriptStepan MazurovView Answer on Stackoverflow
Solution 4 - JavascriptGytisView Answer on Stackoverflow
Solution 5 - JavascriptsiukurninView Answer on Stackoverflow
Solution 6 - JavascriptPeter BaileyView Answer on Stackoverflow
Solution 7 - JavascriptesdebonView Answer on Stackoverflow
Solution 8 - JavascriptakiView Answer on Stackoverflow
Solution 9 - Javascriptuser3319721View Answer on Stackoverflow
Solution 10 - JavascriptmbillardView Answer on Stackoverflow