I want to capture link clicks and store in my database without using a turnstile. Current code
$( ".ads-box a" ).click(function( event ) {
event.preventDefault();
var href = $(this).find('a').attr('href');
var id = $(this).find('a').attr('id')
console.log(href);
console.log(id);
console.log(this);
});
and
<div class="ads-box" id="adbox-35"><a href="http://www.makeitrightnola.org" id="ad-35" target="_blank"><img src="/g/shows/sidebar/83172823_ad_image.png" alt="Make It Right"></a></div>
Produces in console
undefined
undefined
<a href="http://www.makeitrightnola.org" id="ad-35" target="_blank"><img src="/g/shows/sidebar/83172823_ad_image.png" alt="Make It Right"></a>
How do I access the link's attributes? What is going wrong?
Remove the .find('a')
in var href = $(this).find('a').attr('href');
. You are already click"ing" on a <a>
element so it is enough with $(this).attr('href');
Try this:
$( ".ads-box a" ).click(function( event ) {
event.preventDefault();
var href = this.href; // i used vanilla JS here
var id = this.id; // i used vanilla JS here
console.log(href);
console.log(id);
console.log(this);
});