Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
¿Cómo obtener un nuevo valor de entrada con cada pulsación de botón?

Al hacer clic en el botón, muestra una alerta del valor de entrada + una cadena personalizada. Mi problema es que después de hacer clic en el botón y cambiar el valor de entrada, al hacer clic en el botón nuevamente se muestra el valor anterior en lugar del nuevo.

JavaScript

 function test() { var message = document.getElementById("name").value; var newMessage = message + " " + "HELLO!"; document.getElementById("send").addEventListener("click", function (e) { e.stopImmediatePropagation(); alert(newMessage); console.log(newMessage); }); }

HTML

 <!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"> <script src="main.js"></script> <link rel="stylesheet" href="style.css"> <title>Greet your friend</title> </head> <body> <label class="main">Enter the name of your friend you want to greet</label> <input class="main" type="text" name="name" id="name" value="George"> <button class="main" id="send" onclick=test()>DONE</button> </body> </html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tuviste incontables problemas de sintaxis que yo los arreglé. No necesita usar stopImmediatePropagation aquí.

 function test() { var message = document.getElementById("name").value; var newMessage = message + " " + "HELLO!"; // -- You don't need a new variable to apply them -- // message += " " + "HELLO!"; // -- Or this one which is better and newer. Called 'String Literals' -- // message = `${message} HELLO!`; alert(newMessage); console.log(newMessage); };
 <label class="main">Enter the name of your friend you want to greet</label> <input class="main" type="text" id="name" /> <button class="main" id="send" onclick="test()">DONE</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!