Tengo un formulario con tres cuadros de entrada, lo que quiero hacer, ya que ingresaré números en esos cuadros de entrada, quiero ver el cálculo matemático en vivo en el área div
function ABC() { const first = document.querySelector('.first') const second = document.querySelector('.second') const third = document.querySelector('.third') const total = document.querySelector('.math') const ab = first * second const cd = ab % third const ef = ab - cd total.innerHTML = ef } <form> <input class="first" type="text" value="" /> <input class="second" type="text" value="" /> <input class="third" type="text" value="" /> </form> <div class="math" onKeyUp="ABC()"> <div>Debe solucionar al menos dos problemas:
valuekeyup que se active en un div , colóquelo en el elemento contenedor de los elementos de input , es decir, en el elemento de form . function ABC() { const first = document.querySelector('.first').value; const second = document.querySelector('.second').value; const third = document.querySelector('.third').value; const total = document.querySelector('.math') const ab = first * second const cd = ab % third const ef = ab - cd total.innerHTML = ef } <form onkeyup="ABC()"> <input class="first" type="text" value="" /> <input class="second" type="text" value="" /> <input class="third" type="text" value="" /> </form> <div class="math"> <div>Mejor aún:
input , que también se activa cuando realiza cambios a través del menú contextual, arrastrando y soltando texto u otros métodos que no sean de teclado.innerHTML cuando todo lo que quiere hacer es generar texto sin formato .type="number" para sus elementos de entrada. const form = document.querySelector("form"); form.addEventListener("input", ABC); function ABC() { const first = document.querySelector('.first').value; const second = document.querySelector('.second').value; const third = document.querySelector('.third').value; const total = document.querySelector('.math'); const ab = first * second; const cd = ab % third; const ef = ab - cd; total.textContent = ef; } ABC(); <form> <input class="first" type="number" value="1"> <input class="second" type="number" value="1"> <input class="third" type="number" value="1"> </form> <div class="math"> <div>