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

263
Vistas
Onclick event with if condition in javascript html

pretty new to coding. But I have a small assignment where I just not get what function I should do or how I should write it down. Basicly it should give an input box and a button (this works). But now I want to give an alert IF the input is hi and a different alert if the input is bye. For all other inputs there shouldn't happen anything. I'm just not getting it, can someone help out?

<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 Respuestas
Responde la pregunta

0

You should add an event listener to the button, but first you should identify it. To identify it, you have to use document.getElementById("the_id"). Keep in mind that we have other ways to find the element, in other words, we have many selectors (id, class, tag name, etc.).

And when you use an else statement, you do not specify another condition. To specify other condition you have to use else if, because else is for anything that was not in the if or an 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 Denunciar

0

Welcome

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 i f(inputText == "bye") {
      alert("See you later");  
    }
  }
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