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

S L
  • 14,262
  • 17
  • 77
  • 116
simekadam
  • 7,334
  • 11
  • 56
  • 79

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
  • 479,566
  • 201
  • 878
  • 984
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
  • 28,281
  • 7
  • 65
  • 99
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