Diana
Diana

Reputation: 435

Question on $(document).bind()

I have a page on iPad. How do I get the specific element that was touched instead of the generic container element?

$(document).bind("touchstart",function(e){
console.log("touchstart on target : " + e.target.id);
}

Upvotes: 0

Views: 2326

Answers (2)

Brian Grinstead
Brian Grinstead

Reputation: 3490

That seems like it should be working. Here is a small test case that logs out the event based off of http://gregmurray.org/ipad/touch-events.html, and it seems to recognize the touchstart happening on the div. Maybe you could post an example where it is failing?

Upvotes: 0

ThoKra
ThoKra

Reputation: 3029

Try using delegate instead

$(function() {
    $(document).delegate('div', 'click', function(event) {
        alert($(this).attr('id'));

        // To prevent Propagation
        event.stopPropagation()
    });
});

in action: http://jsfiddle.net/xem65/

(Using click since I don't have any touch devices nearby atm)

Docs on delegate: http://api.jquery.com/delegate/

Upvotes: 2

Related Questions