JavaScript dblclick Event
The dblclick
event in JavaScript is triggered when an element is double clicked by the user. This event can be used to perform certain actions on an element when it is double clicked.
Syntax
element.addEventListener('dblclick', function() {
// Code to be executed when the element is double clicked
});
Example
<!DOCTYPE html>
<html>
<body>
<p ondblclick="myFunction()">Double click me</p>
<script>
function myFunction() {
alert("You double clicked the paragraph");
}
</script>
</body>
</html>
Output
When the user double clicks on the paragraph, an alert box will appear that says "You double clicked the paragraph".
Explanation
In the example above, we have defined a paragraph element with the ondblclick
attribute set to myFunction()
. This means that when the user double clicks on the paragraph, the myFunction()
function will be called.
The myFunction()
function simply displays an alert box with the text "You double clicked the paragraph".
Use
The dblclick
event can be used to perform various actions when an element is double clicked. For example, it can be used to open a dialog box, toggle a class on an element, or navigate to a different page.
Important Points
- The
dblclick
event can only be used with interactive elements such as buttons, links, and form elements. - The event can also be attached to an element using the
addEventListener()
method. - The
dblclick
event is not supported in mobile devices.
Summary
The dblclick
event is used to trigger an action when an element is double clicked by the user. It can be used to perform various actions on an element, such as opening a dialog box or navigating to a different page. However, it should be noted that this event is not supported in mobile devices.