I have a link (tag 'a'). I am trying to click it using javascript. Bye .click (); works well in FF en IE, it does not work in Chrome. (chrome says the object has no click method).
running onclick or redirecting to href will not do the job.
any ideas on how to do this? Preferably, I would not get the whole library just for this.
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
In non-IE browsers, use dispatchEvent ()
I understand that you do not want a library, but this size is very small, you can use event.simulate . this is PrototypeJS lib, but I'm sure porting will be very easy.
To use it in a prototype, you must call:
$('my_anchor').simulate('click');
It works cross browser without any problems.
Try to read the link post there, the author has the same problem.He solves it by running the script in the onLoad callback;
<body onLoad="DefaultButtonFix()">
Refernece: Click event in IE / Firefox, but Chrome discards event destination
I use this:
function dispatchEvent(el, e) { // console.log("dispatching"); if ("createEvent" in document) { var evt = document.createEvent("HTMLEvents"); evt.initEvent(e, false, true); el.dispatchEvent(evt); } else { el.fireEvent("on" + e); } } dispatchEvent(document.getElementById("mylink"), "click");
If you know that jQuery just creates an anonymous function and listens for the onclick event. You will need to specify a class or id for href,
eg. HTML:
<a href='' id='clickMe'>Click here</a>
jQuery:
$(function() { $("#clickMe").click(function() { ... the action you wish to perform ... }) })
Hope this helps. Dave
Source: https://habr.com/ru/post/905604/More articles:How to align code in the Eclipse Force.com IDE database - eclipseGet the first line before the delimiter? - stringHow to check registered user using Javascript in sharepoint - javascriptSharepoint 10 Lists: Want to Limit Form Fields to User Groups - sharepoint-2010Where can I place reusable code for controllers and models - zend-frameworkMore objects in memory with the same identifier? - ruby | fooobar.comhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/905606/mercurial-hg-library-for-nodejs-for-local-repositories&usg=ALkJrhiJshzwYHRW3SD73cqiLAOiUUf6bggrail search query - grailsI can't figure out how to set my random number from 1 to 100 - androiddifference between count (*) and count (columnName) - mysqlAll Articles