You are here: irt.org | FAQ | JavaScript | Event | Q1691 [ previous next ]
You can simulate an onDblClick event handler by keeping track of which element was last clicked, and when it was clicked. If the same element is clicked twice in a row, you can fire your own DblClick event. Simply put a call to CheckDblClick() in you onClick event handler. You can vary the double-click time by modifying the value in the condition in the second line of the CheckDblClick() function.
var oLastTime = null;
var oLastClick = null;
function CheckDblClick() {
var oThisTime = (new Date()).getTime();
if (oLastClick == this && oThisTime < oLastTime+750) {
oLastClick = null;
return true;
}
else {
oLastClick = this;
oLastTime = oThisTime;
return false;
}
}
function my_onClickFn() {
if (CheckDblClick())
my_onDblClick.call(this); // Call the DblClick handler
else {
// Single click stuff...
}
}Submitted by Amos Bannister