jQuery Selection

Hi sitepoint-folkz,

im new to your great forum and would like to ask you for help with a problem.

Here is some Dummy-HTML to describe my Problem

<ul>
    <li class="a">Line 1</li>
    <li>Line 2</li>
    <li>Line 3</li>

    <li class="b">Line 1</li>
    <li>Line 2</li>
    <li>Line 3</li>
</ul>

Well… here´s the Problem:

I´d like to select all the <li> from
<li class=“a”>Line 1</li>
to
<li>Line 3</li>
of the first block…

… no i can´t give classes to all the <li> … this would be way to easy :slight_smile:

Thanks for your help

Joe

Will there always be two <li> tags after the one with the class? Or does that vary?

Here’s one way to do it. After dealing with the first element which has a class name - just loop through them until you get to one with a class name


var liTags = document.getElementsByTagName('li'),
    wantedLIs = document.createDocumentFragment(),
    i;
wantedLIs.appendChild(liTags[0]);
while (liTags.length && liTags[0].className === '') {
    wantedLIs.appendChild(liTags[0]);
}
console.log(wantedLIs); // Line 1, Line 2, Line 3

thank you guys for the reply…
@ForceFlow -> not always two, it will vary. Else I Could have used the :nth-child - Selector …

@Paul -> i´ll try using your snippet. Looks good to me.

Greets

Joe