EN
JavaScript - ondblclick event example
0
points
In this article, we would like to show you ondblclick
event example in JavaScript.
Quick solution:
var myElement = document.querySelector('#my-element');
myElement.addEventListener('dblclick', function() {
console.log('ondblclick event occurred.');
});
or:
<button ondblclick="functionName()">double-click me.</button>
or:
var myElement = document.querySelector('#my-element');
myElement.ondblclick = function() {
console.log('ondblclick event occurred.');
};
Practical examples
There are three common ways how to use ondblclick
event:
- with event listener,
- with element attribute,
- with element property.
1. Event listener based example
In this section, we want to show how to use dblclick
event on button
element via event listener mechanism.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<button id="my-button">Double-click me</button>
<script>
var myButton = document.querySelector('#my-button');
myButton.addEventListener('dblclick', function() {
console.log('ondblclick event occurred.');
});
</script>
</body>
</html>
2. Attribute based example
In this section, we want to show how to use dblclick
event on button
element via attribute.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<button ondblclick="handleClick()">Double-click me</button>
<script>
function handleClick(){
console.log('ondblclick event occurred.');
}
</script>
</body>
</html>
3. Property based example
In this section, we want to show how to use dblclick
event on button
element via property.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<button id="my-button">Double-click me</button>
<script>
var myButton = document.querySelector('#my-button');
myButton.ondblclick = function() {
console.log('ondblclick event occurred.');
};
</script>
</body>
</html>