1

I use the following code in CSS

.table>tbody>tr:nth-of-type(even) { background-color:#C6DDFF;}

to color table rows one white, next blue, next white...

But when I use JavaScript to insert a new <tr> (which is needed temporary) the design screws up. Because the CSS sees the inserted <tr> as next row, which is theoretically correct.

But want the new <tr> in a own style, and the original table layout should be the same as in the first load.

Is there a way to tell CSS like "don't affect this row...ignore it?"

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
HKK
  • 237
  • 1
  • 3
  • 17
  • Not using that style, because it will be computed by the browser when the DOM changes. You'll need to copy the styles of the current rows before adding new ones... – BenM Dec 29 '13 at 15:34
  • 1
    You need a more unique way of identifying your existing `tr` tags - give them a class name. – Emissary Dec 29 '13 at 15:35
  • If you just need a temporary table row, could you just do `document.createElement('tr')`, or do you need to do stuff in the page? – Bojangles Dec 29 '13 at 15:55
  • No, its more like a "detail-view" of the parent row. the inserteted row contains a div with details from parent row, gotten by Ajax. – HKK Dec 29 '13 at 15:58

1 Answers1

2

I think this will do the trick.

.table > tbody > tr.someclass:nth-of-type(even) { background-color:#C6DDFF;}

You give your initial trs the someclass and the dynamicly added ones dont get this class.

Works fine: fiddle

UPDATE

When you add rows in the middle of the table this won't work: css3 nth of type restricted to class

Community
  • 1
  • 1
Markus Kottländer
  • 8,228
  • 4
  • 37
  • 61