Search code examples
jqueryclickeventtrigger

Jquery when i click on img it behaves like link (triggering link)


i have a problem, I got some li, inside them i have a link and an image. What i want is when someone clicks on the image it triggers the link. I cant put the image in a link, don't ask why :)... So basically it looks something like this

<li><a href="somelink">sometext</a><img src=""/></li>
<li><a href="somelink">sometext</a><img src=""/></li>
...

And i was thinking of doing something like this:

$(img).click(function(){
  var link = $(this).prev;
  $(link).trigger('click');
})

I know this is not correct, but i think you got the picture, thanks for your help.


Solution

  • $('img').live('click', function () {
        $(this).closest('a').click();
    });