1

I am adding two classes using javascript on my table, the css for the classes is:

//using less
.fade-table {
    background-color: #fff;
    opacity: 0.5;
    &:hover {
        opacity: 1;
    }
}

.selected {
    opacity: 1;
}

what i am trying to achieve here is that, my table fades at the opacity: 0.5 and the selected cell is applied with the selected class which highlights the selected cell.

The javascript being used is:

$("#pending_states table tr").live("click",function(){
    $("#pending_states table").css({width: "140px"});
    $("#pending_states td:nth-child(1), #pending_states th:nth-child(1)").addClass("fade-table");
    $("#pending_states td:nth-child(1), #pending_states th:nth-child(1)").css({width: "140px"});
    $("#pending_states").animate({ marginLeft: "4px"}, 200);
    $(this).addClass("selected");
});

However for some reason after adding the fade-table class the script doesn't apply the selected class to the td. The obvious reason that i can think of is that this doesn't represent the td so o also tried $(this).closest("td").addClass("selected");. However this doesn't seem to work either.

Any suggestions on how this might work?

6
  • All of your nth-child(1) selectors should be replaced with :first. Commented Apr 1, 2012 at 14:24
  • That simply selects the first row. Commented Apr 1, 2012 at 14:26
  • That code works for me: jsfiddle Commented Apr 1, 2012 at 14:28
  • @Pointy, Sorry forgot to mention, the fade-table works fine, however the selected class doesn't Commented Apr 1, 2012 at 14:32
  • Oh sorry - let me play with that too then ... OK I updated the jsfiddle and it still seems to work. The this reference will be the <tr> element, not the <td>. That's because the selector you used references the <tr> explicitly. I think I know what you're getting at so I'll type in an answer. Commented Apr 1, 2012 at 14:34

2 Answers 2

1

If you want to apply "selected" to the <td> that was clicked on, try:

 $("#pending_states table tr").live("click",function(e){
    $("#pending_states table").css({width: "140px"});
    $("#pending_states td:nth-child(1), #pending_states th:nth-child(1)").addClass("fade-table");
    $("#pending_states td:nth-child(1), #pending_states th:nth-child(1)").css({width: "140px"});
    $("#pending_states").animate({ marginLeft: "4px"}, 200);
    ($(e.target).is('td') ? $(e.target) : $(e.target).closest('td')).addClass("selected");
 });

(or something less ugly). The idea is to use the event parameter to find the actual target of the click.

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't it make more sense to change the target of the event rather than trying to find the closest td/th?
@circusbred Yes if this is all that's going on; even if not, the <tr> is pretty easy to get to, and since it's done with delegation it doesn't make much difference really.
0

You are setting opacity on the wrong element. fade-table is applied to the cell, but selected is applied to the row, so the cell will still be set at 50% opacity.

http://jsfiddle.net/UNgbh/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.