Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

77
Views
How to print each array element on a new row

I am getting a new array element from an input and adding to array but I can not print every element in a new row. I can print all elements of array in one row but can not print every element in a different row. I am using <br> after array name but does not work. What is your solution? It is like a todo list project.

var allmembers = [""];

function addnewmember() {
  var newmemberr = document.getElementById("newmember").value;

  allmembers.push(newmemberr);

  for (var i = 0; i < allmembers.length; i++) {
    document.getElementById("membername").innerHTML = allmembers[i] + "<br>";
  }
}
<html>
<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="stil.css">
</head>

<body>
  <input id="newmember" placeholder="NEW MEMBER"><br>
  <button type="button" onclick="addnewmember()">SEND</button>
  <div id="members">MEMBERS</div>
  <div id="membername"></div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your solution is appending a <br> to the end of the array, rather than between array items. Instead of looping over the array, just use the .join() method, which is ideal for something like this.

Additionally, using innerHTML in a loop is a big performance "no no" as it causes the browser to have to repaint and possibly reflow the DOM document repeatedly. In such cases, you should build up a string that contains the HTML you want and after the loop is done set that string as the innerHTML of the desired element in one single command. And really, the use of innerHTML should be avoided if at all possible because of security concerns as well.

See additional comments inline:

let allmembers = [];

// Get your DOM references just once, not every time the function runs
// Make references to DOM elements, rather than their propreties. This
// way, if you decide you need access to a different DOM element property
// you don't have to scan for the element again.
let newmember = document.getElementById("newmember");
let memberName =  document.getElementById("membername");

function addnewmember() {
  allmembers.push(newmember.value);
  newmember.value = "";  // Clear out the input
  
  // No need for a loop. Just join the arry elements
  // with a <br> between them.
  memberName.innerHTML = allmembers.join("<br>");
}
<html>
<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="stil.css">
</head>

<body>
  <input id="newmember" placeholder="NEW MEMBER"><br>
  <button type="button" onclick="addnewmember()">SEND</button>
  <div id="members">MEMBERS</div>
  <div id="membername"></div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!