yzlqpq
Last Updated: February 25, 2016
·
1.319K
· rachelnabors
150 twitter

Map click events to touch events in jQuery

To map click events to "touch events", I put together this based on the feedback here: http://stackoverflow.com/questions/7018919/how-to-bind-touchstart-and-click-events-but-not-respond-to-both

Update Rodney Rehm made a nicer version of this. Thanks Rodney! https://gist.github.com/rodneyrehm/6464641

$.fn.activate = function(runFunc) {

    $(this).on('touchend', function(e){
        $(this).addClass("touched");
        runFunc();
        e.preventDefault();
    });

    $(this).on('click', function(e){
        if (!($(this).hasClass('touched'))) {
            runFunc();
            e.preventDefault();
            e.stopPropagation();
        };
    });

}