How to simulate mouse click using Javascript?

(modified version to make it work without prototype. Js).

(modified version to make it work without prototype. Js) function simulate(element, eventName) { var options = extend(defaultOptions, arguments2 || {}); var oEvent, eventType = null; for (var name in eventMatchers) { if (eventMatchersname. Test(eventName)) { eventType = name; break; } } if (!eventType) throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported'); if (document.

CreateEvent) { oEvent = document. CreateEvent(eventType); if (eventType == 'HTMLEvents') { oEvent. InitEvent(eventName, options.

Bubbles, options. Cancelable); } else { oEvent. InitMouseEvent(eventName, options.

Bubbles, options. Cancelable, document. DefaultView, options.

Button, options. PointerX, options. PointerY, options.

PointerX, options. PointerY, options. CtrlKey, options.

AltKey, options. ShiftKey, options. MetaKey, options.

Button, element); } element. DispatchEvent(oEvent); } else { options. ClientX = options.

PointerX; options. ClientY = options. PointerY; var evt = document.

CreateEventObject(); oEvent = extend(evt, options); element. FireEvent('on' + eventName, oEvent); } return element; } function extend(destination, source) { for (var property in source) destinationproperty = sourceproperty; return destination; } var eventMatchers = { 'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/, 'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/ } var defaultOptions = { pointerX: 0, pointerY: 0, button: 0, ctrlKey: false, altKey: false, shiftKey: false, metaKey: false, bubbles: true, cancelable: true } You can use it like this: simulate(document. GetElementById("btn"), "click"); Credits should go to kangax.

Here's the original source (prototype. Js specific).

Very fast, and nice. I don't normally build safety-checks into my examples like this, I'm impressed that you took the extra effort - great that you made it library-less, too! +1 – Beejamin May 27 at 22:08 Credits should go to kangax, as noted in my answer.

I did make it library agnostic :) – TweeZz May 27 at 22:30.

Here's a pure JS function which will simulate a click (or any mouse event) on a target element: function simulatedClick(target, options) { var event = target.ownerDocument. CreateEvent('MouseEvents'), options = options || {}; //Set your default options to the right of || var opts = { type: options. Click || 'click', canBubble:options.

CanBubble || true, cancelable:options. Cancelable || true, view:options. View || target.ownerDocument.

DefaultView, detail:options. Detail || 1, screenX:options. ScreenX || 0, //The coordinates within the entire page screenY:options.

ScreenY || 0, clientX:options. ClientX || 0, //The coordinates within the viewport clientY:options. ClientY || 0, ctrlKey:options.

CtrlKey || false, altKey:options. AltKey || false, shiftKey:options. ShiftKey || false, metaKey:options.

MetaKey || false, //I *think* 'meta' is 'Cmd/Apple' on Mac, and 'Windows key' on Win. Not sure, though! Button:options.

Button || 0, //0 = left, 1 = middle, 2 = right relatedTarget:options. RelatedTarget || null, } //Pass in the options event. InitMouseEvent( opts.

Type, opts. CanBubble, opts. Cancelable, opts.

View, opts. Detail, opts. ScreenX, opts.

ScreenY, opts. ClientX, opts. ClientY, opts.

CtrlKey, opts. AltKey, opts. ShiftKey, opts.

MetaKey, opts. Button, opts. RelatedTarget ); //Fire the event target.

DispatchEvent(event); } Here's a working example: http://www.spookandpuff.com/examples/clickSimulation.html You can simulate a click on any element in the DOM. Something like simulatedClick(document. GetElementById('yourButtonId')) would work.

You can pass in an object into options to override the defaults (to simulate which mouse button you want, whether shift/alt/ctrl are held, etc. I've tested in FF, Safari and Chrome. IE might need special treatment, I'm not sure.

Nice answer +1'd – Kim Jong Woo Oct 20 at 7:06 This worked great for me, on Chrome, where the elements don't seem to have the click() event. – Howard M. Lewis Ship Oct 25 at 21:39.

Here's a pure JS function which will simulate a click (or any mouse event) on a target element.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions