tags:

views:

39

answers:

3

I am trying to apply css rules to tr elements depending on what class would be the following tr.

ex.

<table>
<tr class="x"/>
<tr class="y"/>
<tr class="x"/>
<tr class="x"/>
</table>

So those tr with class x that have a following tr with class y will have different css style than those tr that the next tr is with class x.

<script type="text/javascript">
 $(document).ready(function () {
  if ($("tr.x").next('(tr.y)')) {

        $(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});

      }
  else{

       $(this).css({'background-color' : 'blue', 'font-weight' : 'normal'});

      }
});
</script>

It is not working at all and I don't know why.

Any help would be appreciated.

Thanks in advance.

Update

I wanted to apply the css style to the first td of the tr, and I am trying;

$(this).first('td').css({ 'color': 'Blue',});

But still apply it to the row.

Thanks again to all.

+2  A: 
T.J. Crowder
I wanted to apply the css style to the first td of the tr, and I am trying;$(this).first('td').css({ 'color': 'Blue',});But still apply it to the row
Cesar Lopez
@Cesar: You're looking for `td:first-child`, I've updated the answer.
T.J. Crowder
@T.J. Crowder: Thanks for your reply, I think I did not explain myself well, I would need to apply style to the first td of each tr that has pass the condition. Once again Thanks.
Cesar Lopez
@Cesar: No worries, that was clear. That's what my update does. Check out the live examples.
T.J. Crowder
@T.J. Crowder: Thanks! It worked, Thank you very much!
Cesar Lopez
@Cesar: No worries. And if you haven't already given Sarfraz an up-vote, I would -- his answers were also on target (much more so than my original was, but I've improved it a lot since then).
T.J. Crowder
@T.J. Crowder: I did give him a vote up :-)
Cesar Lopez
+4  A: 

Try this:

$('.x + .y').prev().css({'background-color' : 'yellow', 'font-weight' : 'bolder'});

Working Demo

Sarfraz
This would style the .y elements, not the .x elements.
Christian Nesmark
@Sarfraz: I took the liberty of updating it for you.
T.J. Crowder
@Sarfaz: Ah, overlapping edits, you wiped mine out with a different, but also working, example.
T.J. Crowder
@T.J. Crowder: Yup i was editing and did not know you had edited already :)
Sarfraz
@Sarfraz: I posted my alternate to the above in my answer. Both are good, kudos again for the `+`.
T.J. Crowder
I wanted to apply the css style to the first td of the tr, and I am trying;$(this).first('td').css({ 'color': 'Blue',});But still apply it to the row
Cesar Lopez
@Cesar: You need to use this code: `$(this).find('td:first').css({ 'color': 'Blue',});`
Sarfraz
@Sarfraz: Thank you for your answer, I am going to use T.J. solution as I understand it better. But I will look into your as it looks really good too.
Cesar Lopez
@Cesar: No problem, his answer is good one too :)
Sarfraz
+1  A: 

You should split this into two: jQuery and CSS. Use CSS to style all the .x elements, and use jQuery to do your special formatting.

$(".y").prev(".x").children("td").first().css({'background-color':'yellow', 'font-weight':'bolder'});

Demo: http://jsbin.com/enova4

Edit: Actually, you should just add a class to your "special" .x elements, and perform ALL styling in CSS. I also adjusted the code to address the first child td.

$(".y").prev(".x").children("td").first().addClass('x-special');

...
<style>
.x-special {
  background-color: yellow;
  font-weight: bolder;
}
</style>
Christian Nesmark
@Christian: Thank your for your answer, at the moment it would only apply to the first td of the first row, I need it to do it for the first td of all rows.
Cesar Lopez
Good point - use children("td:first-child") instead of children("td").first() should do the trick :)
Christian Nesmark