Languages
[Edit]
EN

JavaScript - ondblclick event example

0 points
Created by:
elmer
646

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>

Alternative titles

  1. JavaScript - on double-click event example
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - events

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join