Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate a click on 'a' tag in Chrome

I have a link ('a' tag). I'm trying to click it using JavaScript. While .click(); does well in Firefox and IE but it fails in Chrome. (Chrome says the object does not have the click method).

Triggering the 'onclick' or redirecting to the 'href' won't do the job.

Any ideas on how to do this? Preferably I wouldn't get an entire library just for this.

like image 250
user746379 Avatar asked Jan 10 '12 10:01

user746379


2 Answers

In non-IE-browsers use dispatchEvent()

like image 175
Dr.Molle Avatar answered Nov 14 '22 03:11

Dr.Molle


var event = document.createEvent("MouseEvents");
            event.initMouseEvent("click", true, true, window,
                0, 0, 0, 0, 0,
                false, false, false, false,
                0, null);
            element.dispatchEvent(event);

should work in chrome

like image 32
amal Avatar answered Nov 14 '22 05:11

amal