1

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 ?

3
  • $("body").append("<p>" + list[i] + " </p>") or $("body").append(['<p>',list[i],'</p>'].join()) Commented Apr 1, 2017 at 15:20
  • 1
    basic string concatenation or use ES6 templates Commented Apr 1, 2017 at 15:22
  • should be fairly obvious where problem is when you look at current results Commented Apr 1, 2017 at 15:24

1 Answer 1

1

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>

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

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.