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

286
Vistas
Condition if else shows me the results backwards. What's going on?

I have made a simple if else condition where it should show which is the greater number of the two inputs entered, but it shows me the opposite value than expected. What's going on? Thanks in advance!

HTML:

 <input type= "text" id="texto">
 <input type= "text" id="texto2">
 <button type="submit" id="enviar">Enviar</button>

JS:

const texto = document.getElementById('texto');
const texto2 = document.getElementById('texto2');
const send = document.getElementById('send');
send.addEventListener('click', mayorque)


function mayorque() {

if (texto > texto2){
    
    alert('Input 1 is greater than input 2')
} else {
    alert('Input 2 is greater than input 1')
}

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

0

A few problems.

When you set the variable texto on load, it is grabbing the value of the input on load. You will need to get the value in the function.

Using something like parseFloat will convert the value to a numeric value. Float will allow for decimal points.

let texto = document.getElementById('texto');
let texto2 = document.getElementById('texto2');
const send = document.getElementById('send');
send.addEventListener('click', mayorque)


function mayorque() {
  texto = parseFloat(texto.value);
  texto2 = parseFloat(texto2.value);
  if (texto > texto2) {

    alert('Input 1 is greater than input 2')
  } else {
    alert('Input 2 is greater than input 1')
  }


}
<input type="text" id="texto">
<input type="text" id="texto2">
<button type="submit" id="send">Enviar</button>

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