Link related to image click event in jQuery

I need a click event for jQuery so that when someone clicks on an image on my site, it fires a click event and then sends them to the target link from the "a" tag. How would I do that? I assume the click event will be on the link? Probably no? Here is the link and image

<a href="go/to/here" class="link_for_image">
   <img alt="Image!" class="dynamic" width="200" height="200" src="image/path/here">
</a>

EDIT

I need to do Javascript before sending the user a link. Sorry for the confusion.

+3
source share
1 answer

You do not need to do this. Since the tour image is wrapped inside the anchor tag, when you click on the image, it will be considered a click on the anchor tag.

. javascript , .

$(function(){
 $(".link_for_image").click(function(e){
   e.preventDefault();
   var item=$(this);
   //Here you can do what you want on the clik

   window.location.href=item.attr("href");
 });    
});
+4

All Articles