Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I listen for triple clicks in JavaScript?

If this is for a double-click:

window.addEventListener("dblclick", function(event) { }, false); 

How can I capture a triple-click? This is for a pinned tab in Google Chrome.

like image 873
Sam-Bo Avatar asked Jun 25 '11 19:06

Sam-Bo


People also ask

Is it possible to triple-click?

Along with clicking and double-clicking, triple-clicking allows three different actions to be associated with the same mouse button.

What is the function of triple-click?

triple-click (plural triple-clicks) (computing) The action of pushing the button on a mouse three times in quick succession in order to perform a different task that would be performed from a single-click or double-click.

What is the difference between double-clicking and triple-clicking to select?

If you are working with text, double-clicking a word selects that word. Triple-clicking selects the sentence.

What is double and triple-clicking?

To select a word, double-click it; to select an entire paragraph, triple-click; to select a single line, click once to the left of the line; and to select the entire document, press Ctrl-A.


1 Answers

You need to write your own triple-click implementation because no native event exists to capture 3 clicks in a row. Fortunately, modern browsers have event.detail, which the MDN documentation describes as:

A count of consecutive clicks that happened in a short amount of time, incremented by one.

This means you can simply check the value of this property and see if it is 3:

window.addEventListener('click', function (evt) {     if (evt.detail === 3) {         alert('triple click!');     } }); 

Working demo: http://jsfiddle.net/L6d0p4jo/


If you need support for IE 8, the best approach is to capture a double-click, followed by a triple-click — something like this, for example:

var timer,          // timer required to reset     timeout = 200;  // timer reset in ms  window.addEventListener("dblclick", function (evt) {     timer = setTimeout(function () {         timer = null;     }, timeout); }); window.addEventListener("click", function (evt) {     if (timer) {         clearTimeout(timer);         timer = null;         executeTripleClickFunction();     } }); 

Working demo: http://jsfiddle.net/YDFLV/

The reason for this is that old IE browsers will not fire two consecutive click events for a double click. Don't forget to use attachEvent in place of addEventListener for IE 8.

like image 180
Andy E Avatar answered Sep 21 '22 11:09

Andy E