Click here to Skip to main content
15,905,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello Programmers,
In my project I have a requirement that I want to get all the li tag id's inside a div and display it on alert. I want all those id's from a partial View and call it on my Layout page on a click of a button.

What I have tried:

The page in which all the li tag presents
<big>    <table >
    <tr>
        <td id="UserMenu">
            <ul style="margin-left:590px">
                <li id="menu01User" style="color:red; float:left;padding:5px; list-style:none">
                    <a href="##" style="text-decoration:none">User 1</a>
                </li>
                <li id="menu02User" style="color:red; float:left;padding:5px;  list-style:none">
                    <a href="/Menu/UtilityMenu" style="text-decoration:none">User 2</a>
                </li>
                <li id="menu03User" style="color:red;float:left;padding:5px; list-style:none">
                    <a href="##" style="text-decoration:none">User 3</a>
                </li>
            </ul>

        </td>
    </tr>
</table></big>


The below code is from different file (index)
<input id="btnClick" type="button" value="Processing"/>

    <div id="NestedMenu" style="display:none"></div>



The javaScript file:
$(document).ready(function () {
    var Ids = [];

    $("#btnClick").click(function(){
        $.ajax({
            cache: false,
            type: "GET",
            url: "/Menu/UserMenu",
            success: function (data) {
                debugger;
               $("#NestedMenu").innerHTML=data;
               $("#UserMenu").find("li").each(function () {
                    Ids.push($(this).attr("id"));
                    alert(Ids);
                });
          },
            Error: function (data) {
                alert("Error");
            }  });  });
Posted
Updated 12-Sep-17 0:32am

try
var arrayIDs = [];
           $('#UserMenu ul li').each(function (index,li) {
               arrayIDs.push(li.id)
           });
           alert(arrayIDs.join(',')); //menu01User,menu02User,menu03User
 
Share this answer
 
Given that your javascript file was called on your _Layout file, you could use ".each" method in javascript:
JavaScript
var IDs = [];
$('#UserMenu ul li').each(function(){
   var _id = parseInt($(this).val()); 
   IDs.push(_id);
});
 
Share this answer
 
v2
Comments
Rohit Singh 12-Sep-17 9:32am    
thnx @Cristian

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900