fatcatz
fatcatz

Reputation: 541

Is is possible to treat double clicks as single clicks in JavaScript?

I'm learning JavaScript. I noticed that if I click on an object multiple times, quickly, some clicks are captured as double clicks. Is it possible to capture all clicks in JavaScript as single clicks only?

Upvotes: 3

Views: 724

Answers (2)

Zubair
Zubair

Reputation: 1

There are two options to solve this problem.

1) Using "return false;" statement on double click event. Example:

<button id="your button id" onclick="yourfuntion()" ondblclick="return false;" >My Button</button>

2) Disable the button/object in the start of your main function and in the end of the function enable it again.

Example:

<button id="your button id" onclick="yourfuntion()">My Button</button>

<script>

function yourfuntion() { document.getElementById("your button id").disabled = true; 
//your javascript code 

document.getElementById("your button id").disabled = false;}

</script>

Upvotes: 0

Pat
Pat

Reputation: 25675

Using jQuery, you could create a double click event handler on the document and then prevent that default behavior:

$(document).dblclick(function(e) {
    e.preventDefault();
    alert('Handler for .dblclick() called and ignored.');
});

Double click in the example to see the result.

Upvotes: 3

Related Questions