Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
How do you get the user data from a HTML input (text) created using createElement()?

I have created a calculator that makes a new row every time a button is clicked. In every new row generated are two input fields that users can input numbers and a div to show the resulting amount.

Calculation: (Input1 x Input2) - Input1

What I need help with is:

  1. How to get the user data from input1 and input2 and store them in a variable, like you do when using getElementbyID or queryselector?
  2. How to take that variable and add the final result to the div?

I am new to JavaScript and cannot find any answer that tells me how to target new elements created using the createElement functions. All I can find is how to create, append, remove or add a classList or a textNode.

const input1 = document.createElement("INPUT");
document.body.appendChild(input1);
const input2 = document.createElement("INPUT");
document.body.appendChild(input2);
const result = document.createElement("DIV");
document.body.appendChild(result);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

an idea can be to use EventListener on the created input to manipulate the data

const input1 = document.createElement("INPUT");
document.body.appendChild(input1);
input1.addEventListener('change', function (e) {

  var target = e.target || e.srcElement;
  console.log(target.value);
});

you can also add a selector (id or class) to the created element and recover it by this selector

const input1 = document.createElement("INPUT");
document.body.appendChild(input1);
input1.id = 'test';

function showData() {
  var input = document.getElementById('test');
  console.log(input.value);
}
<button onclick="showData()">show data</button>

with your sample it can look like

const input1 = document.createElement("INPUT");
input1.id = 'input1';
document.body.appendChild(input1);

const input2 = document.createElement("INPUT");
input2.id = 'input2';
document.body.appendChild(input2);

const result = document.createElement("DIV");
result.id = 'result';
document.body.appendChild(result);


function showResult() {
  var input1 = document.getElementById('input1');
  var input2 = document.getElementById('input2');
  var result = document.getElementById('result');
  if (input1 && input2 && result) {
    result.innerText = input1.value * input2.value;
  }
}
<button onclick="showResult()">show result</button>

if you have to dynamically create div and showresult you can also create the button and manipulate the onclick event

function createNewRow() {
    const input1 = document.createElement("INPUT");
    input1.id = 'input1';
    document.body.appendChild(input1);

    const input2 = document.createElement("INPUT");
    document.body.appendChild(input2);

    const result = document.createElement("DIV");
    document.body.appendChild(result);
    
    const button = document.createElement("BUTTON");
    button.innerText = 'show result';
    button.addEventListener('click', function() {
      result.innerText = input1.value * input2.value;
    });
    document.body.appendChild(button);
}
    <button onclick="createNewRow()">create New Row</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

OK, so the Id work, but it only works for that one row. I need to repeat that process (equation) independanly on each new row that's created.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda