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

102
Visualizações
alert box not working properly in JavaScript
<script>
    function fun(){
          let i=document.getElementById("input")
          if(i==="sam")
            {
               alert("welcome SAM")
            }
          else
            {
               alert("welcome user")
            }
        }
      </script>
     
        <input id="input"/>
        <button onclick="fun()">click</click>

if user type input as 'sam' it should be follow the if block but every time it execute else part .how to change the condition that when user type 'sam' in textbox then only it follow if block or else execute else part

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

0

getElementById method returns html element, if you want to get text inside input you should use document.getElementById("input")?.value

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. Make sure your input is correct.

  2. Instead of writing "if(i===sam)", write "if(i=="sam").

  3. Other than that, I think there's a problem with your input method. Javascript doesn't directly get the input from the box. For further information, check this link: https://www.tutorialspoint.com/How-to-take-user-input-using-HTML-forms

  4. If you want to do it the easier way, just put "document.getElementById("input").value"

If this answer helped you, please mark it as an answer

about 4 years ago · Juan Pablo Isaza Relatório

0

Just add .value ahead of document.getElementById("input")

Just like that:

let i=document.getElementById("input").value

Basically, you're not passing the text to the variable i that is the reason else is executing

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