views:

7038

answers:

4

Is it possible to programmatically invoke a onClick-event for an anchor tag, while maintaining the 'this' reference to the anchor?

The following doesn't work... (at least not in Firefox: document.getElementById('linkid').click() is not a function)

<script type="text/javascript">
    function doOnClick() {
        document.getElementById('linkid').click();
        //Should alert('/testlocation');
    }
</script>
<a id="linkid" href="/testlocation" onclick="alert(this.href);">Testlink</a>
+5  A: 

To trigger an event you basically just call the event handler for that element. Slight change from your code.

evnt = document.getElementById("element")["onclick"];

if (typeof(evnt) == "function") {
    document.getElementById("element")["onclick"]();
}
simplyharsh
+1. Also, a bit of googling reveals that foo.click() is IE only, of course I could be wrong...
karim79
evnt.onclick() would actualy work as well
José Leal
You should use dot notation ( .onclick ) rather than than with the bracket notation ( ["onclick"] ).
Rudd Zwolinski
.onclick() or .click() both don't work as expected. In Firefox .click (or bracked ['click']) it is "NOT A FUNCTION". But when calling .onclick() you lose the "this" reference (I need this in ASP.NET MVC because info from the <a> tag is needed in the executing function)
Ropstah
A: 

If you're using this purely to reference the function in the onclick attribute, this seems like a very bad idea. Inline events are a bad idea in general.

I would suggest the following:

function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
     elm.addEventListener(evType, fn, useCapture);
     return true;
    }
    else if (elm.attachEvent) {
     var r = elm.attachEvent('on' + evType, fn);
     return r;
    }
    else {
     elm['on' + evType] = fn;
    }
}

handler = function(){
   showHref(el);
}

showHref = function(el) {
   alert(el.href);
}

var el = document.getElementById('linkid');

addEvent(el, 'click', handler);

If you want to call the same function from other javascript code, simulating a click to call the function is not the best way. Consider:

function doOnClick() {
   showHref(document.getElementById('linkid'));
}
David Caunt
it's about keeping the reference to 'this'. I cannot solely generate the onclick part in the anchor (.NET MVC). I need to generate a complete anchor tag. This tag is required completely by the onclick function (e.g. it uses the href attribute).
Ropstah
+8  A: 

You need to apply the event handler in the context of that element:

var elem = document.getElementById("linkid");
if (typeof elem.onclick == "function") {
    elem.onclick.apply(elem);
}

Otherwise this would reference the context the above code is executed in.

Gumbo
I think you're the only one not seeing me as a fool (for not mentioning the bracket notation / the onclick|click browser incompatibility). Spot on!
Ropstah
consider this: http://www.howtocreate.co.uk/tutorials/javascript/domevents -- will calling the onclick function invoke *all* event handlers registered? Also, falsely invoking an event will not produce the default action associated with that event (eg a form submission).
Here Be Wolves
Gumbo, as this code does produce -different- results, it still doesn't work with the Microsoft.Ajax and Microsoft.AjaxMvc libraries....
Ropstah
A: 

Here is an example to set all child anchor tags onclick event using javascript.

http://urenjoy.blogspot.com/2009/07/set-divs-all-anchor-tags-onclick-event.html

Hope, It helps.