Last Updated: September 09, 2019
·
3.254K
· rachelnabors

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();
        };
    });

}