onlycode.org

javascript
jquery
html
css
angularjs
node.js
ajax
html5
php
json
arrays
google-chrome
dom
regex
reactjs
ecmascript-6
javascript-events

your's history:
how can i programmatical...

How can I programmatically invoke an onclick() event from a anchor tag while keeping the ‘this’ reference in the onclick function, just only code?

javascript | javascript-events

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

32
Gumbo, 25.05.2009 15:35




edit
<script type="text/javascript">
    function doOnClick() {
        $('#linkid').click();
    }
</script>
<a id="linkid" href="/testlocation" onclick="alert(this.href);">Testlink</a>

31
luschn, 27.05.2013 12:59




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

if (typeof(evnt) == "function") {
    evnt.call(a);
}

30
simplyharsh, 25.05.2009 15:21




edit
$("#linkid").trigger("click");

29
bfontaine, 21.11.2015 20:59



edit
function MyObj() {
   this.abc = "ABC";
}
MyObj.prototype.handleEvent = function(e) {
   console.log("caught event: "+e.type);
   console.log(this.abc);
}

var myObj = new MyObj();

document.querySelector("#myElement").addEventListener('click', myObj);
// see above for definition of MyObj

var myObj = new MyObj();

document.querySelector("#myElement").addEventListener('click', myObj.handleEvent.bind(myObj));

13
Rolf, 22.01.2016 19:30




edit
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);
function doOnClick() {
   showHref(document.getElementById('linkid'));
}

12
David Snabel-Caunt, 25.05.2009 15:33




edit
document.getElementsByName('MyElementsName')[0].click();
document.getElementById('MyElementsId').click();

21
Tyler Montney, 27.12.2017 22:52

Add your just only code

your name:    



If you need full text click here

Some similar links How can I programmatically invoke an onclick() event from a anchor tag while keeping the ‘this’ reference in the onclick function? ↓:
HTML anchor tag with Javascript onclick event

How to invoke “click” event programmatically in d3?

: Invoking click-event of an anchor tag from javascript

How do I prevent a parent's onclick event from firing when a child anchor is clicked?



This project is created for those who just need only code, wihout maaaaaaaaaaany words.