How to trigger onClick before href

Viewed 32012

I am trying to do some functionality in onClick() event and also doing the href for the same element. But actually happening is its not triggering onClick() completely and its moving to href link when we click that element.

How to make onClick() gets complete first and trigger href next.

<a href="" onclick="javascript:triggerMe()">click</a>
5 Answers

As I understood, you don't want to go on other page, so use return false, like this:

<a href="" onclick="javascript:triggerMe();return false;">click</a>
<a  onclick="javascript:triggerMe()" href="">click</a>

It will work

 <a href="" onclick="return triggerme()" >click</a>
 function triggerme(e){
    if(condition is true){
       return true;
    } else {
       e.preventDefault();
       return false;
    }  
 }

Try this one will surely work

Related