JavaScript dblclick event - JavaScript Tutorial

JavaScript dblclick event

BLUF: This tutorial on JavaScript dblclick event provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript dblclick event

Understanding JavaScript dblclick event is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The dblclick event triggers an occurrence when an element is clicked two times in rapid succession. This event is activated when an element is double-clicked within a brief timeframe. Additionally, we can utilize JavaScript's addEventListener method to initiate the double click event.

In HTML, the ondblclick attribute can be utilized to generate a double-click event.

Syntax

At this point, let's examine the syntax for establishing a double-click event in both HTML and JavaScript, considering both scenarios: without utilizing the addEventListener method and with the use of the addEventListener method.

In HTML

Example

<element ondblclick = "fun()">

In JavaScript

Example

object.ondblclick = function() { myScript };

In JavaScript by using the addEventListener method

Example

object.addEventListener("dblclick", myScript);

Let’s examine a few examples to gain a clearer understanding of the double-click event.

Example - Using ondblclick attribute in HTML

In this illustration, we are generating the double-click event by utilizing the HTML ondblclick attribute.

Example

<!DOCTYPE html>

<html>

<head>

</head>



<body>

<h1 id = "heading" ondblclick = "fun()"> Hello world :):) </h1>

<h2> Double Click the text "Hello world" to see the effect. </h2>

<p> This is an example of using the <b> ondblclick </b> attribute. </p>

<script>

function fun() {

document.getElementById("heading").innerHTML = " Welcome to the logic-practice.com ";

}

</script>

</body>

</html>

Output

Upon running the aforementioned code, the resulting output will be -

Upon executing a double-click on the phrase "Hello world," the resulting output will be -

At this point, we will explore the process of implementing a double-click event utilizing JavaScript.

Example - Using JavaScript

Example

<!DOCTYPE html>

<html>

<head>

</head>



<body>

<h1 id = "heading"> Hello world :):) </h1>

<h2> Double Click the text "Hello world" to see the effect. </h2>

<p> This is an example of creating the double click event using JavaScript. </p>

<script>

document.getElementById("heading").ondblclick = function() { fun() };

function fun() {

document.getElementById("heading").innerHTML = " Welcome to the logic-practice.com ";

}

</script>

</body>



</html>

Output

Upon performing a double-click on the text "Hello world", the resulting output will be -

Example - Using JavaScript's addEventListener method

Example

<!DOCTYPE html>

<html>

<head>

</head>



<body>

<h1 id = "heading"> Hello world :):) </h1>

<h2> Double Click the text "Hello world" to see the effect. </h2>

<p> This is an example of creating the double click event using the <b> addEventListener() method </b>. </p>

<script>

document.getElementById("heading").addEventListener("dblclick", fun);

function fun() {

document.getElementById("heading").innerHTML = " Welcome to the logic-practice.com ";

}

</script>

</body>



</html>

Output

Upon double-clicking the phrase "Hello world," the resulting output will be -

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience