Reputation: 7599
I'm having a table with several rows - I assigned a hover function to each row. what I want to find out in my hover function 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?
Upvotes: 1
Views: 738
Reputation: 66201
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.
Upvotes: 5
Reputation: 44121
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.
Upvotes: 1
Reputation: 887777
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
.
Upvotes: 3