Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to confirm navigation for a link in a href tag? [duplicate]

I'm using simple links for an admin panel but in case of user accidently clicks on link for user deletion, there should be a confirmation pop-up for avoding this.

Probably as you know a href tag's are not fully compatible with javascript. So, i have used span tag for "onclick" but that didn't went so well:

<script type="text/javascript">
function confirm_click()
{
return confirm("Are you sure ?");
}

</script>

Here is the html code for link:

   <span onclick="confirm_click();">    <a title="Delete User" href="http://www.google.com"><i class="icon-remove-sign">DELETE</i></a></span>
like image 940
mirza Avatar asked Nov 27 '12 11:11

mirza


2 Answers

Try...

<a title="Delete User" onclick="return confirm_click();" href="http://www.google.com"><i class="icon-remove-sign">DELETE</i></a>

...instead.

You need to return the value of your method, and returning false in an onclick event prevents the default behaviour (which in this case would be to navigate to the href).

like image 106
MadSkunk Avatar answered Oct 12 '22 23:10

MadSkunk


Try

<a href='/path/to/action' onclick="return confirm('Are you sure ?')">Delete</a>
like image 45
lostsource Avatar answered Oct 13 '22 00:10

lostsource