Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

Preventing event of link fails

I have got this html on the page:

<a href="review.asp?sitename=webcamking#video_embed" class="tutorials" data_site="webcamking">&nbsp;&nbsp;Tutorial 1</a>

This jquery:

    $('.tutorials').click(function(event){
       event.preventDefault();
       event.stopPropagation()
       var linkText=$(this).text();
       var siteName= $(this).attr('data_site');
       alert("Test. linkText:"+linkText+". siteName:"+siteName);
      _gaq.push(['_trackEvent', 'LinkClicked', siteName, linkText]);
       //window.location.href=$(this).attr('href');

       return false;

  })

The problem is that the link still keeps sending me to the destination. I need to stop it. I need to check what I get with the alert..and hope the google analytics event is triggered!!

Update: Modified code:

$(document).ready(function(){

       $('.tutorials').click(function(event){
       event.preventDefault();
       var linkText=$(this).text();
       var siteName= $(this).attr('data_site');
       alert("Test. linkText:"+linkText+". siteName:"+siteName);
      _gaq.push(['_trackEvent', 'LinkClicked', siteName, linkText]);
       //window.location.href=$(this).attr('href');

       return false;

  });

Upvotes: 0

Views: 59

Answers (2)

kayen
kayen

Reputation: 4868

  1. Did you enclose your jQuery code block in the $( document ).ready( function() {}) statement?
  2. Is there any piece of code above this block that is breaking down and preventing this block from executing?

Upvotes: 2

Oritm
Oritm

Reputation: 2121

Use this:

html:

<div id="tutorials">&nbsp;&nbsp;Tutorial 1</div>​

jquery:

 $('#tutorials').click(function(event){
   event.preventDefault();
   event.stopPropagation()
   var linkText=$(this).text();
   var siteName= $(this).attr('data_site');
   alert("Test. linkText:"+linkText+". siteName:"+siteName);
  _gaq.push(['_trackEvent', 'LinkClicked', siteName, linkText]);
   //window.location.href=$(this).attr('href');

   return false;

})​

Upvotes: 1

Related Questions