3

im trying to print array elements with html li element in a dynamically created div but no errors showing and neither array elements just an empty div.

What could be wrong?

Here is my code:

var fruitsName, fruitsLists, numFruits, i;

fruitsName = ["Mango", "Apple", "Banana", "Strawberry", "Blackberry", "Blueberry"];
numFruits = fruitsName.length;
fruitsLists = "<ul>";

for (i = 0; i < numFruits.length; i++)
{
    fruitsLists = "<li>" + fruitsName[i] + "</li>";
}
fruitsLists += "</ul>";
var myDiv = document.createElement('div');
myDiv.className = 'bookmarksHolder';
myDiv.id = 'randomItem';

document.getElementById('bookmarks_row').appendChild(myDiv);
document.getElementById('randomItem').innerHTML = fruitsLists;
1
  • i forgot to say im new to programming and stackoverflow as well, so i hope for no downvote. Commented Mar 23, 2017 at 9:50

3 Answers 3

3

Two problems: numFruits is already a length of array, so should be just i < numFruits. Second, in loop concatenate previous value with the current:

for (i = 0; i < numFruits; i++) {
  fruitsLists += "<li>" + fruitsName[i] + "</li>";
}

Complete sample:

var fruitsName, fruitsLists, numFruits, i;

fruitsName = ["Mango", "Apple", "Banana", "Strawberry", "Blackberry", "Blueberry"];
numFruits = fruitsName.length;
fruitsLists = "<ul>";

for (i = 0; i < numFruits; i++) {
  fruitsLists += "<li>" + fruitsName[i] + "</li>";
}
fruitsLists += "</ul>";

var myDiv = document.createElement('div');
myDiv.className = 'bookmarksHolder';
myDiv.id = 'randomItem';

document.getElementById('bookmarks_row').appendChild(myDiv);
document.getElementById('randomItem').innerHTML = fruitsLists;
<div id="bookmarks_row"></div>

Sign up to request clarification or add additional context in comments.

Comments

1

You are assigning to fruitsLists in the loop, instead of appending. Also your array length is numFruits, so don't call .length of that. Change your loop to:

for (i = 0; i < numFruits; i++)
{
    fruitsLists += "<li>" + fruitsName[i] + "</li>";
}

You can also give the ul as innerHTML to the div prior to attaching the div to the DOM. This will invoke the rendering engine only once, instead of twice, and you wouldn't have to find the div by ID:

var myDiv = document.createElement('div');
myDiv.className = 'bookmarksHolder';
myDiv.id = 'randomItem';
myDiv.innerHTML = fruitsLists;

document.getElementById('bookmarks_row').appendChild(myDiv);

Comments

0

Within the loop, you need to concatenate instead of updating entire value otherwise fruitsLists includes the last generated HTML of li. Although update the for loop condition since numFruits holds the length.

for (i = 0; i < numFruits; i++)
//             ---------^^^---
{
    fruitsLists += "<li>" + fruitsName[i] + "</li>";
    //      ---^^^---
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.