Caselle di input - Forum

Forum Navigation
You need to log in to create posts and topics.

Caselle di input

good morning, I can't figure out where the problem is.
Inside a div I have 55 input boxes with the same class.
what I want to do is "paste in an input box 55 values/numbers and through a button in which I have inserted this code:
which should insert the numbers in each input box but why does this happen?

function distribuzioneNumeri() {
// Get the value inserted in the numbers field
let input = document.getElementById("inputNumbers").value;

// Remove any extra spaces and separate the numbers (this only considers numbers and commas/spaces)
let numeri = input.split(/[\s,]+/).filter(num => num.match(/^\d+$/)); // Remove words and filter numbers

// Check that there are exactly 55 numbers
if (numeri.length !== 55) {
alert("You must enter exactly 55 numbers!");
return;
}

// Select all text boxes with the class "numero-box"
let caselle = document.getElementsByClassName("number-box");

// Check if there are enough boxes
if (boxes.length < numbers.length) {
alert("There are not enough text boxes!");
return;
}

// Distribute the numbers in the boxes
for (let i = 0; i < numbers.length; i++) {
boxes[i].value = numbers[i]; // Insert the number in the boxes
};

}

I take these numbers from a site of the game of eight, including the name of the wheels, the code should discard the name of the wheels and insert the values ​​in each box.
If you have any suggestions, thanks?

@llazzari VisualNEO Web uses NeoScript for most of the tasks you are trying to do.

For example, you don't need any code for getting the value from a TextInput. It's just stored in the assigned variable.
You can use StrParse to split a string.
Converting to a number is also a one command task using ToNumber.

Anyway, regarding JavaScript:

The issue lies in a few small mistakes in your JavaScript code. Here's a breakdown of what's going wrong and how to fix it:

Issues Identified:

  1. Variable Naming Inconsistencies:

    • You defined the array as numeri, but later referred to it as numbers.
    • Similarly, you selected elements using caselle, but then referred to them as boxes.
  2. No Declaration for boxes:

    • You used boxes.length without defining boxes. You meant to use caselle.

Corrections Needed:

  1. Consistently use either Italian or English for your variable names to avoid confusion.
  2. Ensure that you're using the correct variable names in all parts of your code.

Corrected Code:

function distribuzioneNumeri() {
    // Get the value inserted in the numbers field
    let input = document.getElementById("inputNumbers").value;

    // Remove any extra spaces and separate the numbers
    let numeri = input.split(/[\s,]+/).filter(num => num.match(/^\d+$/));

    // Check that there are exactly 55 numbers
    if (numeri.length !== 55) {
        alert("You must enter exactly 55 numbers!");
        return;
    }

    // Select all text boxes with the class "number-box"
    let caselle = document.getElementsByClassName("number-box");

    // Check if there are enough boxes
    if (caselle.length < numeri.length) {
        alert("There are not enough text boxes!");
        return;
    }

    // Distribute the numbers in the boxes
    for (let i = 0; i < numeri.length; i++) {
        caselle[i].value = numeri[i]; // Insert the number in the boxes
    }
}

 

Key Changes Made:

  1. Replaced numbers with numeri everywhere.
  2. Replaced boxes with caselle.
  3. Checked the length of caselle instead of the undefined boxes.

Additional Suggestions:

  • Make sure that all input boxes have the class number-box.
  • Verify that the input field where you paste numbers has the "inputNumbers".
  • If the numbers are copied from a website, ensure there are no hidden characters or special symbols.
Open chat
1
Do you need more info?
Hi, do you have any doubt?