jianu81
jianu81

Reputation: 115

How do I display an array inside a string in javascript?

This is the code :

list = ["Alex","John","Kit","Lenny"];
for(var i = 0; i < 4; i++) {
  $("body").append("<p> list[i] </p>');
};

Look at the for loop(yes this is using jquery),i want to add the list items inside the paragraph headers.How do i do it ?

Upvotes: 1

Views: 57

Answers (1)

kind user
kind user

Reputation: 41893

list[i] is not a string, it's a variable. To include it into the appended element, close the quotation marks in following way:

var list = ["Alex","John","Kit","Lenny"];
for(var i = 0; i < 4; i++) {
  $("body").append("<p>" + list[i] + "</p>")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions