Home > other >  How to find out which element the user clicked on the touch device
How to find out which element the user clicked on the touch device

Time:08-16

I need to find out which element the user clicked on the touch screen and write its id to the console.

I try using the touchstart event, but the result is always undefined.

Here is my code:

$(document).on('touchstart', function (event) {
    // The element that was clicked.
    var clickTarget = $(event.targetTouches[0]);

    // Log element id
    console.log(clickTarget.attr('id'));
});

I use jQuery in the project so it can be used to solve this issue.

CodePudding user response:

targetTouches won't return the touched dom. You need to get it from target property of it.

$(document).on('touchstart', function (event) {
    // The element that was clicked.
    var clickTarget = $(event.targetTouches[0].target);
});
  • Related