For JS Unit test, I need to check that a double-click behaves as expected. The issue is that the event was registered via element.addEventListener. And for some reason, in this case, element.ondblclick() does not work. HTML:
<input type="image" src="pic.jpg" id="aa"/>
Javasript:
document.getElementById('aa').addEventListener("dblclick", function(){alert('aa')}); document.getElementById('aa').ondblclick();
Fiddle: http://jsfiddle.net/prZKy/
If you double click on the image, it works, but the ondblclick() in the javascript does not work.
Anyone has an idea on how to do it?
The dblclick event fires when a pointing device button (such as a mouse's primary button) is double-clicked; that is, when it's rapidly clicked twice on a single element within a very short span of time.
The dblclick event generates an event on double click the element. The event fires when an element is clicked twice in a very short span of time. We can also use the JavaScript's addEventListener() method to fire the double click event. In HTML, we can use the ondblclick attribute to create a double click event.
If you want to disable any mouse click action use addEventListener(event, function, useCapture) . Onclick ,call this function.
The ondblclick attribute fires on a mouse double-click on the element.
You can use dispatchEvent
to programatically trigger events:
var event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true
});
document.getElementById('aa').dispatchEvent(event);
See the section "Triggering built-in events" on MDN.
Here is a fiddle of the code in action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With