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

264
Views
Evento onclick con condición if en javascript html

bastante nuevo en la codificación. Pero tengo una pequeña tarea en la que simplemente no entiendo qué función debo hacer o cómo debo escribirla. Básicamente debería dar un cuadro de entrada y un botón (esto funciona). Pero ahora quiero dar una alerta SI la entrada es hola y una alerta diferente si la entrada es adiós. Para todas las demás entradas no debería pasar nada. Simplemente no lo entiendo, ¿alguien puede ayudarme?

 <h1>Welcome</h1> Input: <input id="welcome1" type="text" /> <button id="button">Click here</button> </body> <script type="text/javascript"> if (document.getElementById("welcome1").textContent = "hi"){ document.getElementById("button").onclick = alert("Welcome"); } else (document.getElementById("welcome1").textContent = "bye"){ document.getElementById("button").onclick = alert("See you later"); } </script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe agregar un detector de eventos al botón, pero primero debe identificarlo. Para identificarlo, debe usar document.getElementById("the_id") . Tenga en cuenta que tenemos otras formas de encontrar el elemento, es decir, tenemos muchos selectores (id, clase, nombre de la etiqueta, etc.).

Y cuando usa una declaración else , no especifica otra condición. Para especificar otra condición tienes que usar else if , porque else es para cualquier cosa que no estaba en el if o un else if .

 <h1>Welcome</h1> Input: <input id="welcome1" type="text" /> <button id="button">Click here</button> <script type="text/javascript"> const btn = document.getElementById("button") btn.addEventListener("click", function() { if (document.getElementById("welcome1").value === "hi") { alert("Welcome"); } else { alert("See you later"); } }) </script>

about 4 years ago · Juan Pablo Isaza Report

0

Bienvenidos

 Input: <input id="welcome1" type="text" > <button id="button">Click here</button> <script type="text/javascript"> // add eventListener to the element document.querySelector("#button").addEventListener("click", handleButton, false) // function handle to check function handleButton () { // get the value from the input // use querySelector instead of getElementById const inputText = document.querySelector("#welcome1").value; // check if is the same string and the same type (string) if(inputText === "hi") { alert ("Welcome"); } else if(inputText == "bye") { alert("See you later"); } }
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!