javascriptonclick

Javascipt:void(0) but no onclick


While searching for something, came across this article and have no idea how the below code works:

<a class="button eula-download-button" data-g-event="cta" data-g-label="download-chrome" 
    href="javascript:void(0)">Download Chrome</a>

this page uses it: https://www.google.com/intl/en/chrome/browser/

This blog discusses it: http://adactio.com/journal/6022/

My question is, if there is no onClick, how is the action done?


Solution

  • You can use a "listener" to fire when you click the <a> where the "listener" is active.

    For example:

    var el = document.getElementById("t"); 
    el.addEventListener("click", function(){...runcode... }, false); 
    

    This will run a function when you click in the element with the ID name "t"

    In this case the button uses classes, so it could look like this in plain javascript:

    var el = document.querySelector("eula-download-button"); 
    el.addEventListener("click", function(){...runcode... }, false);