1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Get the first <ul> outside of the tree (but nor parent)

Discussion in 'JavaScript' started by trops, Jun 26, 2013.

  1. #1
    Here is my HTML structure:
    SEMrush
    <ul class="ul-main">
        <li>James</li>
        <ul class="ul-sub">
                  <li>Joyce</li>   
                  <li class="widget_subpages_current_page">Randi</li>   
        </ul>
        <li>John</li>
        <li>David</li>
    </ul>
    HTML:
    And now I need to addClass('current-li-main') class to the first <li> element top in hierarchy where widget_subpages_current_page class is. So, it will be <li class="current-li-main">James</li>


    So, the result will be:
    <ul class="ul-main">
        <li class="current-li-main">James</li>
        <ul class="ul-sub">
                  <li>Joyce</li>   
                  <li class="widget_subpages_current_page">Randi</li>   
        </ul>
        <li>John</li>
        <li>David</li>
    </ul>
    HTML:
    How will this jQuery code look like?
     
    trops, Jun 26, 2013 IP
    SEMrush
  2. tonicc

    tonicc Active Member

    Messages:
    198
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    Solution:
    jQuery('.ul-main li').eq(0).addClass('current-li-main');
    Code (markup):
    Regards!
     
    Last edited: Jun 26, 2013
    tonicc, Jun 26, 2013 IP