views:

32

answers:

2

For some reason jQuery.addClass has stoped working. I have no idea why. Other stuff works though.

  // Change row background color on click
  jQuery('#rowList tr').live("click", function() {
    alert(jQuery(this).attr('title')); // Just here for testing. This works
    alert(jQuery(this).css('border','solid 1px red')); // Just here for testing. This works

    jQuery(this).closest("tr").siblings().removeClass("selected"); 
    jQuery(this).addClass("selected");  // NOT working
  });

Any reason why this is not working? Here is my HTML:

<table id="rowList">
  <tbody>
    <tr title="My title 1" class="imageItem odd"> <td>some stuff</td></tr>
    <tr title="My title 2" class="imageItem even"><td> some stuff here</td></tr>
  </tbody>
</table>
+3  A: 

You have a missing ) on the second alert:

alert(jQuery(this).css('border','solid 1px red'));
                                               ^

Once you fix that it works, you can test it here. As an aside, since you're on a <tr>, there's no need for the .closest("tr") call, you can remove it from the chain, something like this overall:

CSS:

.bordered { border: solid 1px red; }

Script:

jQuery('#rowList tr').live("click", function() {
  alert(jQuery(this).attr('title'));
  jQuery(this).addClass("bordered selected")
              .siblings().removeClass("selected"); 
});​

You can give it a go here.

Nick Craver
The alerts are just for testing - and just a typo from me. It's the addClass that is not working. This has been working all along up til now.
Steven
@Steven - It works in my demo...pretty simple behavior here, is it possible your CSS is something like `td.className` and not `tr.className`?
Nick Craver
I removed the two lines with jQuery to add / remove the class, and then it worked.... so there must be something else bugging up my sytem. But thanks for the short code - less overhead.
Steven
BTW. Nice sand box that link of yours.
Steven
A: 

Because the way you've written it is the .addClass() adds the class to the td not the parent tr.

Unless the omission of closest('tr') was a typo?

David Thomas
Of course, @Nick's suggestion seems to be slightly more likely...
David Thomas