views:

39

answers:

1

I have a table whose border is set as 1px solid silver from an external style sheet(some .css file). I have no control over this file.

This is the css: my_table tbody td { font: 8.5pt verdana, sans-serif; border-top: solid 1px silver; border-bottom: solid 1px silver; overflow: hidden; padding: 0px 3px 0px 2px; }

Now I want to change the border color of the last row in the table to black. Im trying to use the following jQuery statement for this.

$('#table_1 tr:last').css('border', '1px solid black');

But this does not seem to work. How can I do this with JQuery/JavaScript?

+1  A: 

If it's the TDs you're after, you need to address them:

$(document).ready(function(){
  $('#table_1 tr:last td').css('border', '1px solid black');
});

This code will wait for the DOM to be ready and then apply the border on the TDs in the last row.

Edit: Seeing your CSS declaration, you need to make the style more specific. Here's how (with the class name):

$(document).ready(function(){
  $('.my_table > tbody tr:last td').css('border', '1px solid black');
});

If worse comes to worse, go for $('table.my_table > tbody tr:last td').

Gert G
Any explanation to that downvote?
Gert G
Something strange happens when I do this:The bottom border only becomes black. Other borders are still silver.But when I use 2px solid black, that gets applied to all borders.Not really sure why this happens.
Sounds strange. Would you mind posting the code? Do you have anything else in the table?
Gert G
Sorry Gert. Your answer is very useful. I did not mean to downvote. Im very new to this forum and I was playing around and that became a downvote. Im trying to undo that. Apologies once again!
This is my css:.my_table tbody td { font: 8.5pt verdana, sans-serif; border-left: solid 1px black; border-right: solid 1px black; border-top: solid 1px silver; border-bottom: solid 1px silver; overflow: hidden; padding: 0px 3px 0px 2px;}My table is very simple. Just has 2 columns and 15 rows. No other javascript/jquery features.'my_table' is the class for the table.Basically last column has grandtotal and has to be prominent. So I want a black border for it. I now use the jquery code you provided
@user343409 Update your question with the CSS and use the code formatting feature. It's much more readable that way.
George Marian
Gert - I'm not absolutely sure, but I think there are issues with IE and styling `tr` and `td` elements. Doing a little testing, and that seems to be the case.
patrick dw
@user343409 - No problem. We all make mistakes.
Gert G
Gert - Nevermind. It is not `<td>` elements. Just `<tr>`. I was having another issue.
patrick dw
@patrick - Yes, it didn't work with TR in Firefox either.
Gert G
Gert - If OP wants to simulate a border on a row, you could always just style the top and bottom with border, then the first and last `td` elements in the row with left and right borders, respectively.
patrick dw
$('table.my_table > tbody tr:last td') also gives the same problem
@patrick - Only problem with that approach is that there would be gaps if there's any cell spacing (there would be gaps).
Gert G
Gert - Very true. I assumed OP has some control over the HTML and that spacing was not otherwise desired. Perhaps too many assumptions! ;o)
patrick dw
@user343409 - It seems that with one pixel, the bottom of the second last row overwrites the top of the black box. With two pixels black, it won't happen: http://jsfiddle.net/Dq6Rn/
Gert G
@patrick - Here's an example with the full row: jsfiddle.net/RDYTH (code not optimized)
Gert G
@Gert,Patrick - Thanks! I understand why borders don't appear correctly now