Element with onClick and onDblClick

1

I have a list and in this list I am trying to distinguish a click from a double click

I initially used HTML :

<a class = "alinhar1" href="#" ondblclick="list.oneDblClick();"  onClick="list.oneClick();">

But whenever I double click , click is also called.

I've tried using a flag to try to block click access, but to no avail.

I've also thought about just using onClick and then inside the method check if there was another click next but also unsuccessful

Is there a solution or an alternative to help me overcome this deceit?

    
asked by anonymous 04.06.2015 / 12:53

1 answer

1

You can use the type of the event to unselect them, and you call an intermediate function that routing to the one you want.

More or less like this:

<a class = "alinhar1" href="#" ondblclick="clickHandler(event)"  onClick="clickHandler(event)">'

and then the function:

function clickHandler(e){
    if (e.type == 'dblclick') list.oneDblClick(e);
    else if (e.type == 'click') list.oneClick(e);
}

I'm not sure that browsers support dblclick but the modern ones support all I think.

Edit:

I noticed now that both events are fired in Chrome. Here's a solution to this that waits half a second to make sure what the event is:

var list = {
    oneClick: function (e) {
        alert('um clique do tipo: ' + e.type);
    },
    oneDblClick: function (e) {
        alert('dois clique do tipo: ' + e.type);
    },
    timeout: null
};

function clickHandler(e) {
    (function () {
        var type = e.type;
        var evt = e;
        var verificador = function () {
            list.timeout = null;
            if (type == 'dblclick') list.oneDblClick(evt);
            else if (type == 'click') list.oneClick(evt);
        }
        if (list.timeout) clearTimeout(list.timeout)
        list.timeout = setTimeout(verificador, 500);
    })();
}

jsFiddle: link

    
04.06.2015 / 13:33