harvy666
harvy666

Reputation: 3

Adding an array's content to an unordered list as list elements

So i got this list

<ul>
  <li> This is</li>
  <li> a very nice</li>
  <li> list</li>
</ul>

and this code to put the content of the array into the list

var nevek =["tom", "bob","kate"];
nevek.forEach(element => {
    $('ul').append( " <li> element  </li>");

But instead of adding the content from the array, it just adds 3 new listitems like this

How should I refer to the individual element in the array to print them out?

Upvotes: 0

Views: 175

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28414

You should append the element at each iteration instead of the string, this can be done using template literals as follows:

var nevek =["tom", "bob","kate"];
nevek.forEach(element => {
    $('ul').append( ` <li> ${element}  </li>`);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
     <li> This is</li>
     <li> a very nice</li>
     <li> list</li>
</ul>

Upvotes: 1

Related Questions