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

212
Views
Make table via value

How do I do that JavaScript will print in my HTML page a table via the value the user will choose?

That the JS script:

let numCol = document.getElementById('txtColumns').value;
let numRow = document.getElementById('txtRows').value;
let go = document.getElementById('btn');
let table = document.getElementById('table');

let td = "<td></td>" * numCol;
let tr = ("<tr>" + td + "</tr>") * numRow;

go.addEventListener('click', function(){
    table.innerHTML = tr;
})

That the HTML code:

<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <table class="workTable">
            <tr>
                <td>
                    <input type="number" placeholder="Columns Number" id="txtColumns">
                </td>
                <td>
                    <input type="number" placeholder="Rows Number" id="txtRows">
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <button id="btn">
                        Print
                    </button>
                </td>
            </tr>
            <div>
                <table id="table">
                    <!--Here I want to print the table-->
                </table>
            </div>
        </table>
        <script src="script.js"></script>
    </body>
</html>

At first I thought about that way with the script but its only appear as a NaN and not table...

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

0

The following syntax:

let td = "<td></td>" * numCol;

does not produce numCol cells, so the following syntax:

let tr = ("<tr>" + td + "</tr>") * numRow;

does not produce numRow rows also.

So, the whole source code should be:

let go = document.getElementById('btn');
let table = document.getElementById('table');

go.addEventListener('click', () => {
  let numCol = document.getElementById('txtColumns').value; //Get the value of txtColumns at the button click moment.
  let numRow = document.getElementById('txtRows').value;

  let td = "",
    tr = "";
  for (let i = 0; i < numCol; i++) {
    td = td + "<td></td>";
  }
  for (let i = 0; i < numRow; i++) {
    tr = tr + "<tr>" + td + "</tr>";
  }

  table.innerHTML = tr;
})
<table class="workTable">
            <tr>
                <td>
                    <input type="number" placeholder="Columns Number" id="txtColumns">
                </td>
                <td>
                    <input type="number" placeholder="Rows Number" id="txtRows">
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <button id="btn">
                        Print
                    </button>
                </td>
            </tr>
            <div>
                <table id="table" border="1">
                    <!--Here I want to print the table-->
                </table>
            </div>
        </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!