Gyro Gearloose Gyro Gearloose - 5 years ago 133
HTML Question

How to use javascript click method with modifiers (ctrl, shift)

I need to trigger an ajax-ed element by javascript method:

<h:panelGrid id="g" columns="1" onmouseover="window.document.getElementById(aa).click()">


and it works, except that it doesn't recognize the modifiers.

I looked here http://www.w3schools.com/jsref/met_html_click.asp and modified the try it yourself to look like

<input type="checkbox" id="myCheck" onmouseover="myFunction(event)" onclick="alert('click event occured '+event.ctrlKey)">


but
ctrlKey
always is
false
.

Is there a way to persuade the click-method to use and forward the modifiers?

Edit: to clarify the "moused" object and the object that should send the ajax-request are different!

Answer Source

When you click with the Control key, it sends the contextmenu event instead of the click event. If you want a general event for all types of mouse clicks, use mousedown.

<input type="checkbox" id="myCheck" onmousedown="alert('click event occurred ' + event.ctrlKey);">

See what's the equivalent of jquery's 'trigger' method without jquery? for a more detailed answer showing how to synthesize mouse click events, including all the different event parameters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download