/    Sign up×
Community /Pin to ProfileBookmark

Double Click OR Single Click in a table?

I am trying to detect both single click and double click in a table.

In my attempt the single click overrides the double click

Is there a way to trap both single and double clicks in the same table row?

https://jsfiddle.net/agwr76yv/

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 21.2022 — This issue is discussed here:

https://stackoverflow.com/questions/5497073/how-to-differentiate-single-click-event-and-double-click-event

kyw's solution is fairly sophisticated but in case of double click both events, double and single clicks are identified. Seems to me, in order to make the double click override a single click, the solution based on a timer is required.
Copy linkTweet thisAlerts:
@sibertauthorMar 24.2022 — > @Sempervivum#1643297 Seems to me, in order to make the double click override a single click, the solution based on a timer is required.

Yes, if you want to clear click and double click this may be needed. But I can think of this as a benefit that single click selects the table row and double click is doing something else with this row.

I tried to implement this in my code and getting some odd behavior

  • 1. The single click is not fired first time. (double event?)

  • 2. The popup menu classList is not changed.


  • https://jsfiddle.net/41y08zbn/11/

    Any tip how to fix this?
    Copy linkTweet thisAlerts:
    @SempervivumMar 24.2022 — When assigning an event listener inside the callback of another one, the behavior is difficult to predict.

    I changed your code to this:
    ``<i>
    </i> document.addEventListener('click', event =&gt; {
    const row = event.target.closest('tr');
    let moreClicked = false;
    if (row) {
    console.log('click on row');
    if (event.detail === 1) {
    if (event.target.classList.contains('more')) {
    id = row.dataset.id;
    popup(event.target);
    moreClicked = true;
    console.log('1st click on more')
    }
    } else if (event.detail === 2) {
    console.log('double click on row')
    }
    }
    if (!moreClicked) {
    popupmenu.classList.add('hide');
    }
    });<i>
    </i>
    ``

    and it works as expected. However I do not know what behavior you intend to achieve.
    ×

    Success!

    Help @sibert spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.26,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...