Can I manually click on a dom object?

I am using addthis and they have a bunch of confusing code that I hate to delve into inline javascript. I would like to “click” one of their buttons on my page without having to touch the user.

So, essentially, there is <a>one that contains their button (which their built-in javascript processes when the user clicks on it). I would like to “click” for them using my own javascript, but I don’t see anything like the built-in clickMe () function associated with the anchor tag object.

Is it possible to do this, or is it missing from the specification due to security issues?

+3
source share
5 answers

, , , , , .

( 100% -) - , href .

...

self.location.href = document.getElementById("addThisFbLink").href;

, .

@kooilnc, , , , - , .

+1

AddThis, onclick div a. id div, , . click() a.

var id = 'atic_facebook'; //for example
document.getElementById(id).onclick();
+3

jQuery trigger():

. , .

$('a').trigger('click');
+1

This is the function to programmatically trigger an event in a DOM element:

function eventFire(el, etype){
   if (el.fireEvent) {
     (el.fireEvent('on' + etype));
   } else {
     var evObj = document.createEvent('Events');
     evObj.initEvent(etype, true, false);
     el.dispatchEvent(evObj);
   }
}
// usage
eventFire(document.getElementById('someHref'),'click');
eventFire(document.getElementById('someElement'),'mouseover');
// etc.
+1
source

You can directly call the onclick function of an element:

var as = document.getElementsByTagName('a'), i;
for (i=0; i<as.length; i++) {
  if (as[i].onclick) {
    as[i].onclick(); // Run the "onclick" function.
  } else if (as[i].href) {
    window.location = as[i].href; // Browse to the URL.
  } else {
    // Not much we can do!
  }
}
0
source

All Articles