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

270
Vistas
Automatic change of input fields through Event Handler "onkeyup"

I am pretty bad at JS but I need some help at a task in order to prepare for a small exam on web technology.

The task:

I have to write a code where two input fields have to be displayed. The sum of both of the input fields have to be 100. So the input fields will be mainly used for typing in some numbers.

When I type a number in the first input field between 0 - 100 there should be displayed the remaining amount of 100 in the second input field after typing the last number of the first number. This should be also working vice versa. So it should be irrelevant which input field I type in the number. Our professor suggests us to use the event handler "onkeyup".

One example:

First Input field: 3 -> typed in

Second Input field: 97 -> will be shown automatically after typing 3

Please don't laugh, here is my code:

<!DOCTYPE html>
<meta charset="UTF-8">
<html>
    <head>
        <script>
            function calc() {
                var firstnum = document.getElementById("firstprop").value;
                var secondnum = document.getElementById("secondprop").value;
                var firstresult = 100 - parseInt(secondnum);
                var secondresult = 100 - parseInt(firstnum); 
                if(firstnum >=0){
                    secondnum = secondresult;
                }
                if(secondnum >=0){
                    firstnum = firstresult;
                }
            }
        </script>
    <head>
    <body>
        <input type="text" onkeyup="calc()" id="firstprop"/>
        <input type="text" onkeyup="calc()" id="secondprop"/>
    </body>
</html>

Thank you very much for your help. I appreciate it, really :)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here you are

<!DOCTYPE html>
<meta charset="UTF-8">
<html>

<head>
  <script>
    function calc(value, index) {
      if (index == 1) {
        document.getElementById("secondprop").value = 100 - value;
      } else if (index == 2) {
        document.getElementById("firstprop").value = 100 - value;
      }
    }
  </script>

  <head>

    <body>
      <input type="text" onkeyup="calc(this.value, 1)" id="firstprop" />
      <input type="text" onkeyup="calc(this.value, 2)" id="secondprop" />
    </body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

function calc(e) {
            if (e.target.id === "firstprop") {
                var secondElement = document.getElementById("secondprop");
                var value1 = e.target.value ? e.target.value : 0;
                secondElement.value = 100 - (parseInt(value1));
            }

            if (e.target.id === "secondprop") {
                var secondElement = document.getElementById("firstprop");
                var value2 = e.target.value ? e.target.value : 0;
                secondElement.value = 100 - (parseInt(value2));
            }
        }
    </script>
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