Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
If I have an array containing 'li', how do I display it in the body?
Posted

1 solution

Step 1: First you keep a empt div inside body.

HTML
<div id="MyList" style="display:none">
</div>


Step 2:


You must be using any event inside your script tag. I mean, Like change, click or onload or document.ready some event. Itarate through your array.
JavaScript
var html = '<ul>';
for(var item in YourArray)
{

html += item;  // If it has <li> inside, otherwise use html += '<ul><li>' + item + </li></ul>;

}

html += '</li></ul>'

var myDiv = document.getElementById('MyList');
myDiv.innerHtml = html;
myDiv.style.display = 'block';


Now if you want to iterate through a list of li in ul without array, the above for loop should be replaced with a 'each' condition.

JavaScript
var html = '';
Like : $('ul > li').each(function(){

html += this;

});

$('#MyList').text(html);
 
Share this answer
 
v2

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