Anyname Donotcare
Anyname Donotcare

Reputation: 11393

How to make nested loops through controls to get their attributes (client side)?

I have the following aspx :

<div id="columns" runat="server">
       <ul id="column1" class="column" >
        <!-- /////////////// -->
        </ul>
         <ul id="column2" class="column" runat="server">
            <li id="l91" class="widget color-red" runat="server" style=""> 
                     <div class="widget-head" style="cursor: move;">
                      <div class="edit-box" style="display: none;">
                      <div class="widget-content"> 
            </li>
        </ul>
        <ul id="column3" class="column" runat="server">
        </ul>
        <ul id="column4" class="column" runat="server">

        </ul>
    </div>

How can through java script i loop through the div id="columns" to get all the ul except ul id="column1" then loop through each ul to get the following attributes of each li :

id , class , the id of the ul of this li

I try to do this in server side but face specific problem . so i hope some help to get these info client side in array to store them later in the database

Upvotes: 1

Views: 425

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337570

You need to use the not() method to exclude element(s) from your selector, try this:

$("#columns ul").not("#column1").each(function() {
    // do what you need with each ul here
    var ulId = $(this).attr("id");
    var ulClass = $(this).attr("class");

    $("li", $(this)).each(function() {
        // do what you need with each child li here
        var liId = $(this).attr("id");
        var liClass = $(this).attr("class");
    });
});

Upvotes: 3

Travis J
Travis J

Reputation: 82287

<script type="text/javascript">
 function looping(){
  var topDiv = document.getElementById("columns");
  var uls = topDiv.getElementsByTagName("ul");
  for(ul in uls){
   if(uls[ul].id == "column1")continue;
   var lis = uls[ul].getElementsByTagName("li");
   for(li in lis){
    alert("Class: " + lis[li].className + " Id: " + lis[li].id);
   }
  }
 }
</script>

Upvotes: 1

Related Questions