incognito2
incognito2

Reputation: 1106

grab url of a image link using jquery click function

I'm trying to use jquery and click() to grab the url of an image link once its clicked. The html looks like this.

<div class="lp-element lp-pom-image" id="lp-pom-image-167" >
<a href="http://url.com/page" target=""><img src="/image.jpg" alt=""></a>
</div>

The javascript I'm using so far looks like this but it isn't grabbing the url of the clicked image link. The url needs to be passed to where the variable this is.

jQuery(function() {
jQuery("#lp-pom-image-167").click(function() {
trackOutboundLink(this, 'AddToCart', 'Bottom CTA');
return false;
});
}); 

How do I pull the href url with the appropriate base URI? ie. "http://url.com/page"?

EDIT:clarified what i am trying to pull out.

Upvotes: 1

Views: 4053

Answers (3)

Maksym Kozlenko
Maksym Kozlenko

Reputation: 10363

jQuery(".lp-pom-image").click(function() {
  console.log($("img", this).attr("src"));
  return false;
}); 

See a working demo at http://jsbin.com/ebicax/4/

Upvotes: 0

WooCaSh
WooCaSh

Reputation: 5212

Use code below instead of this if you want get a url:

$(this).find("a").attr('href')

or this code if u want get img src:

$(this).find("a img").attr('src')

Upvotes: 0

David Thomas
David Thomas

Reputation: 253446

I'd suggest (though untested):

$('.lp-pom-image').click(function(e){
    e.preventDefault(); // stops the default action,
                        // without preventing propagation/bubbling
    trackOutboundLink($(this).find('img').prop('src'), 'AddToCart', 'Bottom CTA');
});

Upvotes: 1

Related Questions