SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code HTML4Strict:
    <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

    Thanks for your help

    Joe

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Will there always be two <li> tags after the one with the class? Or does that vary?
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    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

    Code javascript:
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •