Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
La salida JS es NaN

¿Puede alguien ayudarme con esto? Soy nuevo con javascript.

 let x1 = parseInt(document.getElementById("x1").value) let x2 = parseInt(document.getElementById("x2").value) word = function(){ let c = x1+x2 document.getElementById("res").innerHTML=c }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Calculator</title> <link rel="stylesheet" href="../css/style.css"> </head> <body> <h1 class="text-4xl">Calculator</h1><br> <div> <form action="GET"> <label for="x1">Input 1</label><br> <input type="text" id="x1" placeholder="1-10.." class="border-2 border-black"><br> <label for="x2">Input 2</label><br> <input type="text" id="x2" placeholder="1-10.." class="border-2 border-black"><br> </form> <button type="button" id="btn" class="rounded-lg bg-gray-500 p-1 px-2 my-2 text-white hover:bg-gray-600" onclick="word()">Test</button> <p id="res"></p> </div> <script src="../js/config.js"></script> </body> </html>

Entonces quiero sumar x1 con x2, pero la salida es NaN.

Aquí está el código en html parece una página html

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Necesita obtener el valor dentro de la función de clic.

 let x1 = document.getElementById("x1") let x2 = document.getElementById("x2") word = function(){ let c = parseInt(x1.value)+parseInt(x2.value) document.getElementById("res").innerHTML=c }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Calculator</title> <link rel="stylesheet" href="../css/style.css"> </head> <body> <h1 class="text-4xl">Calculator</h1><br> <div> <form action="GET"> <label for="x1">Input 1</label><br> <input type="text" id="x1" placeholder="1-10.." class="border-2 border-black"><br> <label for="x2">Input 2</label><br> <input type="text" id="x2" placeholder="1-10.." class="border-2 border-black"><br> </form> <button type="button" id="btn" class="rounded-lg bg-gray-500 p-1 px-2 my-2 text-white hover:bg-gray-600" onclick="word()">Test</button> <p id="res"></p> </div> <script src="../js/config.js"></script> </body> </html>

about 4 years ago · Juan Pablo Isaza Relatório

0

Simplemente poniendo el value dentro de su función.

 let x1 = document.getElementById("x1") let x2 = document.getElementById("x2") word = function(){ console.log(x1.value); let c = parseInt(x1.value)+ parseInt(x2.value) document.getElementById("res").innerHTML= +c }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Calculator</title> <link rel="stylesheet" href="../css/style.css"> </head> <body> <h1 class="text-4xl">Calculator</h1><br> <div> <form action="GET"> <label for="x1">Input 1</label><br> <input type="text" id="x1" placeholder="1-10.." class="border-2 border-black"><br> <label for="x2">Input 2</label><br> <input type="text" id="x2" placeholder="1-10.." class="border-2 border-black"><br> </form> <button type="button" id="btn" class="rounded-lg bg-gray-500 p-1 px-2 my-2 text-white hover:bg-gray-600" onclick="word()">Test</button> <p id="res"></p> </div> <script src="../js/config.js"></script> </body> </html>

about 4 years ago · Juan Pablo Isaza Relatório

0

por lo general, NaN ocurrirá cuando intente hacer operaciones matemáticas número con cadena o cadena con número. por favor, depure y verifique si su código está haciendo eso.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda