Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the first element of several sets

I have an awkward problem, due to inheriting a shedload of really, really badly formatted HTML.

Basically I have some table rows like this:

<tr class="odd">...</tr>
<tr class="odd">...</tr>
<tr class="odd">...</tr>
<tr class="even">...</tr>
<tr class="even">...</tr>
<tr class="even">...</tr>
<tr class="odd">...</tr>
<tr class="odd">...</tr>
<tr class="odd">...</tr>

And what I need is a jQuery selector that will give me the first element of each block of classes. Is that clear enough? I'm not sure how else to explain it.

So in the example case I would want rows 1, 4, and 7.

Ive done this so far by selecting every nth child, but Ive now realised this wont work as there wont always be the same number of rows in each block of classes.

Any help you guys can give it appreciated, as always :)

Cheers!

like image 278
Tyler Durden Avatar asked May 15 '14 14:05

Tyler Durden


People also ask

How do you find the set of the first element?

Collection c; Iterator iter = c. iterator(); Object first = iter. next(); (This is the closest you'll get to having the "first" element of a Set .

How do you extract the first element of a list?

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).

How do I find the first element of an ArrayList?

To get the first element of a ArrayList, we can use the list. get() method by passing 0 as an argument. 0 is refers to first element index.

How do you extract the first element of a list in Python?

In Python lists are zero-indexed, so the first element is available at index 0 . Similarly, we can also use the slicing syntax [:1] to get the first element of a list in Python.


2 Answers

You'll need to employ some strategic use of :not() and adjacent sibling selectors:

$('tr.odd:first-child, tr.even:first-child, tr:not(.odd) + tr.odd, tr:not(.even) + tr.even')

The tr:not(...) + tr... bits select any tr element that's directly preceded by a tr that does not have the same class name. You can't do this dynamically (e.g. with some sort of wildcard), but it's entirely possible if you specify each class name separately. This will ensure you only select the first element in each contiguous group.

The reason the :first-child pseudo is required is simply because + won't match the first child, since it implies a relationship between an element and its preceding sibling. Note that :first-child accounts for the very first tr only. You could replace tr.odd:first-child, tr.even:first-child with simply tr:first-child if every tr element will have exactly one of either class name, but I specify them anyway for clarity's sake.

Last but not least, this doubles as a valid CSS selector, so you can use it in a stylesheet as well if you like.

like image 81
BoltClock Avatar answered Oct 06 '22 01:10

BoltClock


$('tr.odd:not(.odd+.odd),tr.even:not(.even+.even)')

This just takes any .odd element which isn't following another .odd, and any .even element which isn't following another .even element.

like image 21
Denys Séguret Avatar answered Oct 05 '22 23:10

Denys Séguret