NotMyFaultSir NotMyFaultSir - 1 year ago 82
jQuery Question

Set onClick function without firing said function

I have an a-html-element which has a onclick function

<a id="tourTrigger" onclick="javascript:tourfunction()"/>

Now during the time where the function is executed I want to disable the link so I do this in my function:

document.getElementById("tourTrigger").onclick = null;

Now when my function is finished i want to set the onclick on its initial value:

document.getElementById("tourTrigger").onclick = tourfunction();

My problem with this solution is that tourfunction() is fired at this place so i create an endless loop.

Anybody got any ideas? JQuery also is fine.
Thanks for your help

Answer Source

Just don't invoke the function:

 document.getElementById("tourTrigger").onclick = tourfunction;

But I would advice to use addEventListener instead:

document.getElementById("tourTrigger").addEventListener('cli‌​ck', tourfunction); 

since the latter allows to add multiple callbacks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download