0

I have a little problem with jQuery .live method. I am using it for catching ajax events for Google Analytics on my website, but in case I have a link with an inner image, the click event is fired up from the image and my live binded click event does not catch it.

I really dont like to add these events manually everytime after changing content and I dont like to bind it to the image (because of the missing href parameter, this case I had use some .parent method), so what is the best way how to handle this?

Notice: I am not sure about efficiency of the .live method, so in case there are big performance differences, please tell me that:) I tried to profile it in webkit profiler, but I didn't see any difference..

Santosh Linkha
  • 13,916
  • 17
  • 75
  • 114
simekadam
  • 7,154
  • 11
  • 52
  • 77

3 Answers3

0

Just place a click(function(event) { ... }) handler on the static parent element, and find the element which started the event with event.target.

alex
  • 460,746
  • 196
  • 858
  • 974
0

Assuming you have an a containing an img, any event on the img should bubble to the a, which will catch it.

Adam Hopkinson
  • 27,556
  • 7
  • 64
  • 93
0

You could also try using the .delegate() method (http://api.jquery.com/delegate/)

Here is some more info regarding .live() vs. delegate():

Update:

Here is a post by Jupiter 24 about "Why you should never use jQuery live":

Community
  • 1
  • 1
oif_vet
  • 206
  • 2
  • 7