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

241
Visualizações
Why is if block executing and not the else block in javascript?

Learning HTML,CSS & JavaScript. I am wondering why is the following code not working even when I enter the correct data. error.

Here's the code:

    <!DOCTYPE html>
<html lang="en">
<head>
    <title>Test</title>
</head>
<body>
    Username:
    <br>
    <input type="text" name="" id="username"> 
    <br>
    Password:
    <br>
    <input type="text" id="password">
    <br>
    <button onclick="validate();">Submit</button>

    <script>
        // verification
        function validate(){
            var a=document.getElementById(username);
            var b=document.getElementById(password);
            // even if i enter the correct credentials only the if part runs.
            if(a!="admin" || b!=1234){
                alert("Incorrect username or password.\nRetry Please!");
                // opening the same page
                window.open("jsvalidate.html","_self");
            }
            else{
                alert("Welcome" + a);
                // opening some other page.
                window.open("new.html","_self");
            }
        }
    </script>
</body>
</html>

And No I am not creating a validation page I know it should be done with ajax or php, I'm just wondering why it's not working. Please provide me an explanation so that I remember this for my life. Thanks!

about 4 years ago · Juan Pablo Isaza
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