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

262
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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