views:

160

answers:

3

hi all,

i'm having a table with several rows - i assigned a hover function to each row. what i wanna do is find out in my hover funtion if the selected TR is odd or even.

i used this code:

alert(tr.is(":odd"));

unfortunately it doesn't work although it should(?) i'm always getting "false".

i tried getting the rowIndex directly from the TR element like:

alert(tr.is(":odd")+"/"+tr.get(0).rowIndex);

strange thing: i'm getting the correct rowIndex, but always False from the :odd property.

what's wrong?

+2  A: 

The :odd selector removes even-numbered elements from a set.

When called in .is(), the set that it looks at contains only your single tr element. Since it will be at index 0 in that set, it will always be :even.

SLaks
+1  A: 

That's odd. :)

But seriously, a single item is always item 0 of that list, which is even. To have even/odd distinctions, you need to have a list. What most people do is run some on-load function that adds a class to the odd elements.

Randal Schwartz
+4  A: 

The :odd pseudo selector depends on the element being selected within a context or result set. For instance: $('ul li:odd') would select odd elements from that context. I would suggest using this test instead:

var odd = (tr[0].rowIndex % 2 == 0);

Every other row will return true from that expression. Since rowIndex is zero-based, we use value % 2 == 0. If it were one based, you would use value % 2 == 1 to get the odd rows.

Doug Neiner
makes sense. thanks!
Fuxi