Hi I am trying to create programmatically the following HTML section in a specific div, by inserting the Dropdown name and Drop Item. Thank you for any help.
<ul class="nav">
<li class="button-dropdown">
<a href="javascript:void(0)" class="dropdown-toggle">Dropdown 1</a>
<ul class="dropdown-menu">
<li>
<a href="#">Drop Item 1</a>
</li>
</ul>
</li>
</ul>
In simple, I would like to create 1 or 2 methods which accepts 2 strings, 1 for the Dropdown and the other for the Drop Item
try this:
var str='<ul class="nav"><li class="button-dropdown">';
str+= '<a href="javascript:void(0)" class="dropdown-toggle">Dropdown 1</a>';
str+= '<ul class="dropdown-menu"><li><a href="#">Drop Item 1</a></li></ul></li></ul>';
$('#div-id').html(str);
div-id
is your specific div ID .
as @Nihar Sarkar pointed but with the function with the two strings
function updateList(Dropdown1,DropItem1List){
var ListOfItems = '';
for(var i=0; i<DropItem1List.length;i++){
ListOfItems += '<li><a href="#">'+DropItem1List[i]+'</a></li>';
}
var htmlCode = '<ul class="nav">'
+'<li class="button-dropdown">'
+'<a href="javascript:void(0)" class="dropdown-toggle">'+Dropdown1+'</a>'
+'<ul class="dropdown-menu">'
+ListOfItems
+'</ul>'
+'</li>'
+'</ul>';
$('#yourDivId').html(htmlCode);
}
or if the html already exists in your page
$('#yourDivId .nav .button-dropdown a.dropdown-toggle').text(Dropdown1);
$('#yourDivId .nav .button-dropdown ul.dropdown-menu').text(DropItem1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.