tags:

views:

37

answers:

2

I have:

$('th a').click(function() {
   var $th = $(this).closest('th');
   $th.toggleClass('selected');

How do I say:

if ($th('.selected')) {
    alert('selected');
} else {
    alert('not selected');
}
+5  A: 

You're looking for the hasClass method:

if ($th.hasClass('selected'))  //No dot

In general, you're looking for the is method:

if ($th.is('.selected'))      //Yes dot

Since the is method takes a selector, you need to include a ..

SLaks
+1  A: 

hasClass():

if ($th.hasClass('selected')) {
    alert('selected');
} else {
    alert('not selected');
}
zombat