Created
March 24, 2019 02:09
-
-
Save zkmark/b12c749c24ed8fc4a6ff07b9c513a15d to your computer and use it in GitHub Desktop.
List nested Jquery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
//"parentId": null, | |
var menuItems = { | |
"menu-6": { | |
"id": "menu-6", | |
"link_text": "Docs", | |
}, | |
"menu-7": { | |
"id": "menu-7", | |
"link_text": "Contact sub1", | |
"parentId": "menu-6", | |
}, | |
"menu-8": { | |
"id": "menu-8", | |
"link_text": "Contact sub2", | |
"parentId": "menu-6", | |
}, | |
"menu-10": { | |
"id": "menu-10", | |
"link_text": "Contact last sub sub", | |
//"parentId": null, | |
}, | |
}; | |
var url = "#"; | |
function recurseMenu(parent) { | |
var s = '<ul>'; | |
for (var i in menuItems) { | |
if ( menuItems[i].parentId == parent) { | |
s += '<li><a href="' + url + '">' + menuItems[i].link_text + '</a>'; | |
s += recurseMenu(menuItems[i].id); | |
s += '</li>'; | |
} | |
} | |
return s + '</ul>'; | |
} | |
$("#listContainer").html(recurseMenu()); | |
//add on html div with id listContainer | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://codereview.stackexchange.com/questions/88433/creating-a-menu-as-nested-unordered-lists-from-json-data