Jquery - Auto Increment ID's for Nested List

I have a nested list that resembles this:

<div id="treeview-left">
<ul>
   <li>Country
       <ul>
          <li>Region
             <ul>
              <li>District
                 <ul>
                    <li>Group
                       <ul>
                          <li>People</li>
                       </ul>
                    </li>
                  </ul>
              </li>
             </ul>
          </li>
        </ul>
    </li>
 </ul> 
 </div>

This list is dynamically generated, and I need to auto increment ID's for each list item at each level.

Eg. Country li's would have #Lv1-1 , #Lv1-2, #Lv1-3 Region li's would have #Lv2-1 , #Lv2-2, #Lv2-3

Each level needs to start with at 0 or 1, and increment the id based on it's index in that specific ul.

This is my current code, I am unable to even get the first level working.

<script>
                 $(function () {
                     $("#treeview-left > ul li").each(function () {
                         var TopPosition = $(this).index();
                         console.log(TopPosition);
                         $(this).id("Lvl1"+TopPosition);
                     });
                 });
            </script>

Your help is appreciated.

Thanks

Answers


  $(function () {
      $("#treeview-left ul").each(function (i, item) {
          var Tp = i + 1;
          $(this).find('li').each(function (j, item) {
              $(this).attr('id', "Lvl" + Tp + '-' + (j + 1));
          });
      });
  });

Need Your Help

PHP Apple Enhanced Push Notification read error response

php ios push-notification apple-push-notifications

In PHP, how do you use fread() to check if there is an error response when sending enhanced push notifications?

Is it possible to have a space in the title of a column in a temporary table?

sql

I am trying to create the names of columns in a temporary table with spaces. The reason I need these spaces in the name is so that it connects properly with our asp.net application that will be using

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.