Search code examples
javascriptonclickhamllink-to

call javascript function from haml link_to


I would like to invoke a javascript function (without JQuery) when the onclick even fires from a link_to tag in haml. How can I do this?


Solution

  • I think this could work:

    link_to "your link", href, :onclick => "jsFunction"
    

    Or, doing everything JS side:

    document.body.addEventListener('click',function(e)
    {
        var target = e.target || e.srcElement;
        if (target.tagName.toLowerCase() !== 'a')
        {
            return e;//not clicked on link
        }
        //a link was clicked, maybe check class or id or other stuff to narrow it down
        //if a link you were interested in was clicked:
        return theClickFunction.apply(target,[e]);
    },false);