views:

74

answers:

3

Hiya,

I have the following code:

 $(".perf-row").click(function(e) {
   if (e.target.tagName.toLowerCase() == "a") {
  pageTracker._trackPageview($(this).attr("rel"));
  return true; //link clicked
   } else {
  window.open($(this).attr("rel"));return false;
   }
 });

to the conditional statment of if (e.target.tagName.toLowerCase() == "a") i would like to also add "OR class is equal to price".

I'm not quite sure how to do that, i can't seem to find the class of the clicked element (which will be a td inside the table row).

i've tried $(this).class, $(this.class) but neither work....

Help would be greatly appreciated!

+2  A: 

To see whether the element has class .price, use the $.hasClass() method.

$(this).hasClass("price");
Jonathan Sampson
No dot prefix is needed here.
Joel L
You're right, Joel. I was thinking in terms of selectors. Updated :)
Jonathan Sampson
A: 

you can try:

$(this).attr("class");

but this will return you names of all the classes applied to the element.

TheVillageIdiot
Since jQuery has a method for testing class, this is just an incorrect answer, though it does return the classes.
Doug Neiner
A: 

You could combine both your tests into a single jQuery command:

if( $(e.target).is('.price, a') ) { .... }

That tests if the target is either has the class price, or is an a tag, or is both. If you wanted to test that it was an a tag and had the price class, you would use this:

if( $(e.target).is('a.price') ) { .... }

Update: I may have misread your question. If you were going to add this second test to the same if test, then use my code. If you wanted to add a else if then use the .hasClass method as explained by the other answers.

Doug Neiner