Red
Red

Reputation: 2246

Link wrapped around an image click event in Jquery

I need a click event for jQuery so that when someone clicks on an image on my site, it will run the click event and then send them to the destination link from the "a" tag. How would I do this? I assume the click event would be on the link? Maybe not? 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 some Javascript before I send the user to the link. Sorry for the confusion.

Upvotes: 3

Views: 2618

Answers (1)

Shyju
Shyju

Reputation: 218922

You don't need to do that. Since tour image is wrapped inside an anchor tag, when you click on image, it will be considered as a click on the anchor tag.

EDIT : From your comment, you want to do some javascript before the actual link works

$(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");
 });    
});

Upvotes: 4

Related Questions