0

I am a student just learning javascript. I am working on a simple assignment to create an array, create a function that will add the array values to a list. Here is my code attached. In my javascript for statement, I added "document.write(places[i] + "
");", however this only show a list of the values. It doesn't show up in the ul section on my webpage. Can you help correct this line of code. Any help would be appreciated.

<body>
<header>
<h1>
Hands-on Project 3-3
</h1>
</header>

<article>
<h2>Scouting Locations</h2>
<div id="results">
<ul>
<li id="item1"></li>
<li id="item2"></li>
<li id="item3"></li>
<li id="item4"></li>
<li id="item5"></li>
</ul>
</div>
</article>
<script>
//array storing places to display
var places = ["Atlanta", "Nashville", "Dallas", "Los Angeles", "Miami"];

// function to generate list from array
function processPlaces() {
    var liElements = document.getElementsByTagName('li');
    for (var i = 0; i < 5; i++) { 
        // write each array element in places to its corresponding list item in liElements
        document.write(places[i] + "<br />");
    }
}
</script>
</body>
</html>
1
  • What you want to do exactly? Commented Feb 11, 2017 at 5:19

3 Answers 3

1

There are some cool things you can do with javascript and arrays.

So you have your basic for loop above, but here are some tricks.

for (var i = 0; i < places.length; i++)

In this example, you don't have to know the length of your array. Since arrays are zero-indexed, it will iterate 0 through the last item in your array (in your case index 4).

Now, your html, you can delete all your <li> tags, and change the opening <ul> to something like <ul id="places-list"></ul>

Don't worry about it being empty, you will fill it.

Use set of code to create new elements and append them to the DOM.

for (var i = 0; i < places.length; i++) {
  var listItem = document.createElement("li"); //creates new <li>
  var output = places[i]; //saves value of array at this index
  listItem.appendChild(output); //appends array value to <li>
  document.getElementById("places-list").appendChild(listItem); //appends <li> to <ul>
}

You don't need to wrap this loop in a function, unless you plan on calling the function afterwards, such as processPlaces();

I'm glad you are learning and things get much easier as you keep working. Then, when you learn jQuery, this gets even easier.

EDIT IN RESPONSE TO COMMENT:

Just change your code to the following if you don't want to change the html as written:

    for (var i = 0; i < places.length; i++) {
        var listItem = document.getElementById("item" + i); //grabs relevant <li>
        var output = places[i]; //saves value of array at this index
        listItem.appendChild(output); //appends array value to <li> as is
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. You explained this well, however, I am trying to keep the code as written and just have the output replace each list item with the places in the array. Hope that makes sense.
0

Set .innerHTML of liElement at index i within for loop

for (var i = 0; i < 5; i++) { 
// write each array element in places to its corresponding list item in liElements
  liElements[i].innerHTML = places[i] + "<br />"
}

Comments

0

You can use following snippet to create new <ul> element with your array list in your HTML.

 <html>
  <body>
  <header>
<h1>
Hands-on Project 3-3
</h1>
  </header>
     <article>
   <h2>Scouting Locations</h2>
    <div id="results">
    <div id="renderList"></div>
  </div>
 </article>
<script>
(function(){
var ul = document.createElement('ul');
ul.setAttribute('id','proList');

var t, tt;
places = ["Atlanta", "Nashville", "Dallas", "Los Angeles", "Miami"];

document.getElementById('renderList').appendChild(ul);
places.forEach(renderplacesList);

function renderplacesList(element, index, arr) {
    var li = document.createElement('li');
    li.setAttribute('class','item');

      ul.appendChild(li);

    t = document.createTextNode(element);

    li.innerHTML=li.innerHTML + element;
  }
 })();
 </script>
 </body>
</html>

1 Comment

Thank you. This version worked also. I just added liElements[i].innerHTML = places[i] + "<br />" as given above.

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.