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

298
Views
How to dispaly display() without deleting the other HTML
var students = [
  ["test", "test", "test"],
  ["test", "test", "test"]
];
students.sort();

function display() {

  for (i = 1; i < students.length + 1; i++) {
    document.write("<tr>");
    document.write("<td>" + (i) + "</td>");
    document.write("<td>" + students[i - 1][0] + "</td>");
    document.write("<td>" + students[i - 1][1] + "</td>");
    document.write("<td>" + students[i - 1][2] + "</td>");
    document.write("<td><input type='number' class='form-control' id='quiz" + i + "' ></td>");
    document.write("<td><input type='number' class='form-control' id='reqt" + i + "'></td>");
    document.write("<td><input type='number' class='form-control' id='exam" + i + "'></td>");
    document.write("<td><input type='number' class='form-control' id='pg" + i + "' readonly></td>");
    document.write("</tr>");
  }
}

function AddData() {
  var AddName;
  var AddCourse;
  var AddBDay;
  var Data;

  AddName = document.getElementById('Addname').value;
  AddCourse = document.getElementById('AddCourse').value;
  AddBDay = document.getElementById('AddBDay').value;

  if (AddName != "" && AddCourse != "" && AddBDay != "") {
    Data = [AddName, AddCourse, AddBDay];
    students.push(Data);
    console.log(students);

    display();

  } else {
    alert("Invalid Input");
  }

}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You haven't posted any HTML, but assuming you have a <table id="x"> or <tbody id="x">, modify the display function as below:

function display() {

  let html = '';
  students.map((item, i) => {
    html += "<tr>";
    html += "<td>" + (i+1) + "</td>";
    html += "<td>" + item[0] + "</td>";
    html += "<td>" + item[1] + "</td>";
    html += "<td>" + item[2] + "</td>";
    html += "<td><input type='number' class='form-control' id='quiz" + i + "' ></td>";
    html += "<td><input type='number' class='form-control' id='reqt" + i + "'></td>";
    html += "<td><input type='number' class='form-control' id='exam" + i + "'></td>";
    html += "<td><input type='number' class='form-control' id='pg" + i + "' readonly></td>";
    html += "</tr>";
  });  
  document.getElementById("x").innerHTML = html;
}

Also, document.write is rarely to never used. More here.
element.innerHTML is generally used when you want to perform some DOM manipulation as a result of an API call or any other event, as in your case.

about 4 years ago · Juan Pablo Isaza Report

0

document.write is very very old-school and it's very rarely used any more for many reasons.

You can either create a string variable that you can then add (concatenate) new strings to, or you can use more modern methods like in the example below which uses map to iterate over the array to create a new array of strings, and then join that array up into one HTML string.

const students = [
  ["test", "test", "test"],
  ["test", "test", "test"]
];

function getHtml(students) {
  return students.map(arr => {
    return `
      <tr>
        <td>${arr[0]}</td>
        <td>${arr[1]}</td>
        <td>${arr[2]}</td>
      </tr>
    `
  }).join('');
}

const table = document.querySelector('table');
table.innerHTML = getHtml(students);
<table></table>

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!