0

How can I be able to separate my array values with line breaks rather than commas?

It looks like this now: apples,bananas,carrots

I want it to look like this:

apples
bananas
carrots

This is my code:

function insertItem () {

  groceryList.push (foodInput.value);
  groceryList.toString();

  for (var i = 0; i < groceryList.length; i++) {
  document.getElementById("printedList").innerHTML = groceryList;
  }

  foodInput.value = "";

}

function deleteItem () {
  groceryList.splice(1,0);
  document.getElementById("printedList").innerHTML = groceryList;
}
2
  • 3
    Use .join on your array: groceryList.join("\n"); (or possibly groceryList.join('<br/>');). Commented Sep 9, 2015 at 18:39
  • 1
    Why not use more structured HTML, like <ul>? Commented Sep 9, 2015 at 18:48

1 Answer 1

1

I'm going to assume that groceryList is the array you are trying to print. If all you're doing is printing the list in a DOM element, you could do this:

document.getElementById("printedList").innerHTML = groceryList.join('<br/>');

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

1 Comment

sorry, yes -- groceryList is my array. Your suggestion worked like a charm! THANKS!!

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.