Search code examples
jqueryiosjquery-mobileuiwebviewtap

jQuery Mobile on iOS - Wrong target on tap event


I'm working on displaying some text in an UIWebView on iOS. I'd like to get the content of the HTML element tapped by the user. If he taps on a paragraph for example, a popup shows the paragraph content. I'm using jQuery Mobile to achieve this. Here's what I'm doing :

$('body').tap(function(e) {
    var tappedElement = $(e.target);

    alert(tappedElement.text());  
});

It works for the first tap. The second tap no matter where it is, returns the same text.

The event seems to be ok but the target property of the second tap is wrong. On the third tap all comes back to normal and so on ... What am I missing here ? thx


Solution

  • You can try this approach:

    $('*').bind('touchstart', function(e) {
        //this will prevent all elements below the clicked item from firing as well.  
        event.stopPropagation();
        //display element text if available
        alert($(this).text());
    });