0
button.onclick = function () {console.log("done");}; 
button.ondblclick = function () {console.log("done");};

If I want to decrease these lines and make them do the same function without any duplication, how can I do so?

I have a way:

function print ()
{
    console.log("click");
}

button.onclick = print;
button.ondblclick = print;

But I don't want to duplicate the calling of the function. I was thinking something like this:

if (button.onclick == True  || button.ondblclick == True)
{
  console.log("done");
}

Can this be done?

7
  • button.onclick = button.ondblclick = print
    – Andrew Li
    Jul 28, 2017 at 13:50
  • You can but you can't avoid triggering click event on doubleclick. Jul 28, 2017 at 13:51
  • dbl click will trigger the function 3 times. 2 times for each click and 1 time for dbl click
    – Sagar V
    Jul 28, 2017 at 13:54
  • @AndrewLi how does your suggestion or the dupe address not duplicating the action? it's essentially the same thing the op says doesn't work
    – aw04
    Jul 28, 2017 at 13:58

0

Browse other questions tagged or ask your own question.