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

166
Views
Auto-fill problem with my table generator

I'm trying make table generator for HTML with JavaScript. When I tried generate, the first field is created successfully. But auto-filling doesn't work correctly.

Error with image

How can I solve this problem?

My Library code:

export default class Table {
  #maxlength
  constructor(body) {
    this.body = body
    this.parent = document.createElement('table')
    this.body.append(this.parent)
    this.#maxlength = 0;
  }
  add(...values) {
    let element = document.createElement('tr')
    let elname = this.parent.childElementCount < 1 ? "th" : "td"
    function add(value) {
      let addval = document.createElement(elname)
      addval.textContent = value
      element.append(addval)
    }
    [...values].map(el => {
      add(el)
    })
    if(this.#maxlength - [...values].length > 0) {
      new Array(this.#maxlength - [...values].length).map(el => {
      add("")
    })
      
    }
    if (this.#maxlength < [...values].length) {
      this.#maxlength = [...values].length
    }
    this.parent.append(element)
  }
}

My HTML Javascript:

import Table from './Table.js'

const { body } = document;

let table = new Table(body)
table.add("a","a","a")
table.add("a","a","a")
table.add("a","a","a")
table.add("a","a") // AUTOFILLING
table.add("a") // AUTOFILLING
table.add("a","a") // AUTOFILLING
table.add("a","a","a")
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It appears you were creating the th or td elements but they were never added to the tr element. I believe this was primarily due to the fact that you weren't actually appending the empty entries into values as intended. Comparing values.length to this.#maxlength in a while loop resolves this issue as seen in the code snippet below:

class Table {
  #maxlength;
  constructor(body) {
    this.body = body;
    this.parent = document.createElement('table');
    this.body.append(this.parent);
    this.#maxlength = 0;
  }
  add(...values) {
    let element = document.createElement('tr');
    let elname = this.parent.childElementCount < 1 ? "th" : "td";
    // Add blank entries for null values
    while (values.length < this.#maxlength) {
      values.push("");
    }

    function add(value) {
      let addval = document.createElement(elname);
      addval.textContent = value;
      element.append(addval);
    }
    [...values].forEach(el => {
      add(el);
    });
    if (this.#maxlength - [...values].length > 0) {
      new Array(this.#maxlength - [...values].length).forEach(el => {
        add("");
      });
    }
    if (this.#maxlength < [...values].length) {
      this.#maxlength = [...values].length;
    }
    this.parent.append(element);
  }
}

const {
  body
} = document;

let table = new Table(body);
table.add("a", "a", "a");
table.add("a", "a", "a");
table.add("a", "a", "a");
table.add("a", "a"); // AUTOFILLING
table.add("a"); // AUTOFILLING
table.add("a", "a"); // AUTOFILLING
table.add("a", "a", "a");
table,
th,
td {
  border: 1px solid black;
}

th,
td {
  width: 1.5rem;
}

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!