views:

103

answers:

1

When I use jQuery for a simple click event it only works for links. Is there a way to make it work for spans etc:

$("span.clicked").live("click", function(e){alert("span clicked!")});

$("a.clicked").live("click", function(e){alert("link clicked!")});

Example is here: http://ip1n.j-www.com/test/index2.html

The SPAN works in Safari but not Mobile Safari (on iPhone or iPad) whereas the A tag works in both.

+1  A: 

You need to listen for the "touchstart" and "touchend" events. Add the listeners with jQuery...

$('span').bind( "touchstart", function(e){alert('Span Clicked!'} );

You may wish to listen for a touchstart and touchend so that you can verify that the element targeted when the finger touched is the same as the element targeted when the finger was removed.

I'm sure there is probably a better way to do it but that should work :)

Sam Nicholson
Thanks. Does this mean that iPhone ignores click events except to links (perhaps because of the other possible gestures) ?
Gazzer
I think that because <a /> tags work the way they do i.e. they're there to be clicked - mobile webkit handles the click event on them automatically. I don't know this for sure. It's just an assumption
Sam Nicholson