Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jquery traversal

how can i target the 2nd ul and the succeeding ones in this bootstrap nav using jquery?

    <div id="navbar" class="navbar-collapse collapse">

      <ul class="nav navbar-nav">

        <li class="active"><a href="#">Home</a></li>

        <li><a href="#about">About</a></li>

        <li><a href="#contact">Contact</a></li>

        <li class="dropdown">

          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>

    <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">

          <li><a href="#">Some action</a></li>

          <li><a href="#">Some other action</a></li>

          <li class="divider"></li>

          <li class="dropdown-submenu">

            <a tabindex="-1" href="#">Hover me for more options</a>

            <ul class="dropdown-menu">

              <li><a tabindex="-1" href="#">Second level</a></li>

              <li class="dropdown-submenu">

                <a href="#">Even More..</a>

                <ul class="dropdown-menu">

                    <li><a href="#">3rd level</a></li>

                  <li><a href="#">3rd level</a></li>

                </ul>

              </li>

              <li><a href="#">Second level</a></li>

              <li><a href="#">Second level</a></li>

            </ul>

          </li>

        </ul>

        </li>

      </ul>

    </div><!--/.nav-collapse -->

2 Answers

Hey Ryan,

Based on your document structure, I came up with an easy, simple way to target the 2nd level unordered list and any unordered lists below it. You can use this as a foundation and then, if you need to, target specific unordered lists within these lists.

//this code grabs all unordered lists below that 1st level unordered list
//you can chain this further if you need to be more specific 
//and think there might be another ul with a class of nav down the line:
//var uls = $('ul.nav.navbar-nav').find('ul');
var uls = $('ul.nav').find('ul');

//each iterates over a jQuery object, in this case the uls we got from the find function
//index is an optional parameter that can be used to target a specific index
//index can be renamed to any valid variable name
uls.each(function (index) {
   //some sample code to show that this is only targeting those 2nd and lower unordered lists
   $(this).css('background-color', 'lightblue');
});

worked like magic. thanks man. really appreciate it :)

My pleasure, Ryan! Happy Coding! =]