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

115
Views
Javascript breaks when using values from HTML input field

I'm trying to create a grid of a specific height/width given input. If I hard code in the height and width in the canvas below (10 and 5 in this case) it works perfectly

const canvasWindow = document.getElementById("canvas-window")
const height = document.getElementById("height")
const width = document.getElementById("width")
const generateCanvasBtn = document.getElementById("generate-canvas-btn")

generateCanvasBtn.addEventListener("click", () => {

    const canvas = new Canvas(document.getElementById("canvas-window"), 10, 5);
    canvas.generateCanvas();
})

This example breaks though:

const canvasWindow = document.getElementById("canvas-window")
const height = document.getElementById("height")
const width = document.getElementById("width")
const generateCanvasBtn = document.getElementById("generate-canvas-btn")

generateCanvasBtn.addEventListener("click", () => {
    const canvas = new Canvas(document.getElementById("canvas-window"), height.value, width.value);
    canvas.generateCanvas();
})

Any ideas what could be causing this? If I log height.value and width.value it is getting the values just fine, so I'm struggling to see how it's any different than being hardcoded if the values are being read in fine? The error I'm getting is:

canvas.js:18 Uncaught TypeError: Cannot set properties of undefined (setting '0') at Canvas.generateCanvas (canvas.js:18:33) at HTMLInputElement. (pixelcreator.js:8:12)

Here is code containing the line that the error references:

class Canvas {

grid;

constructor(window, height, width) {
    this.window = window;
    this.height = height;
    this.width = width;
}

generateCanvas() {
    this.grid = new Array(this.height).fill(0).map(() => new Array(this.width).fill(0));
    for (let i = 0; i < this.height; i++) {
        const row = document.createElement("div");
        row.classList.add('row');
        this.window.appendChild(row);
        for (let j = 0; j < this.width; j++) {
            this.grid[i][j] = new Cell(row) // this is line 18 from the error
            this.grid[i][j].createCell();
        }
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

height.value or width.value might be a string type.

Try this:

new Canvas(document.getElementById("canvas-window"), parseInt(height.value), parseInt(width.value));
about 4 years ago · Juan Pablo Isaza Report

0

You are using the value of the input directly which is a string. Try parsing it to an integer with parseInt(...)

const canvasWindow = document.getElementById("canvas-window")
const height = document.getElementById("height")
const width = document.getElementById("width")
const generateCanvasBtn = document.getElementById("generate-canvas-btn")

generateCanvasBtn.addEventListener("click", () => {
    const canvas = new Canvas(document.getElementById("canvas-window"), parseInt(height.value), parseInt(width.value));
    canvas.generateCanvas();
})
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!