How to simulate a click or keydown event?

I need to simulate a click or mouse event. I tried different things, but the lib that I use does not seem to respond to it or does not respond, but only on certain browsers. ATM i do $ ('# target'). Val ($ ('# target2'). Val ()); which works on firefox and opera. Chrome, IE8 and Safari don't work.

I could add events to the libraries, but I did not know which event to add (or how to do it correctly). Anyway, how can I solve this? I basically install textarea text with .val (), and lib does not seem to pick up this event.

+4
source share
3 answers

You can use the DOM Level 2 event model, for example:

function simulateClick(element) { var evt = document.createEvent("MouseEvents"); evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); cb.dispatchEvent(element); } 

Demo: http://www.jsfiddle.net/4yUqL/66/

This really mimics mouseclick on an element. No matter how the events were connected.

+3
source

.trigger('click') in jQuery can achieve what you are trying to do. It will kill all handlers attached to the click event.

+2
source

In case someone comes across this, looking for a way to agnostic framework to fire any HTML and Mouse event, look here: How to simulate a mouse click on JavaScript?

+1
source

Source: https://habr.com/ru/post/1335994/


All Articles