Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$(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.

>Solution :

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);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading