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

151
Visualizações
Javascript is not running in HTML file

can someone be happy to help me, in my code below I'm trying to display a div with a dropdown select, but why is the existing javascript not working.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #business {
  display: none;
}
#local {
  display: none;
}

    </style>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script>
        $('#purpose').on('change', function () {
                if (this.value === "1") {
                    $("#business").show();
                    $("#local").hide();
                } else if (this.value === "2") {
                    $("#business").hide();
                    $("#local").show();
                } else {
                    $("#business").hide();
                    $("#local").hide();
                }
            });
    </script>
</head>
<body>
    <select id='purpose'>
        <option value="0">Personal use</option>
        <option value="1">Business use</option>
        <option value="2">Passing on to a client</option>
    </select>
    <div id="business">
        <label for="business">Business Name</label>
        <input type='text' class='text' name='business' value size='20' />
    </div>
    <div id="local">
        <label for="local">Local Name</label>
        <input type='text' class='text' name='local' value size='20' />
    </div>
</body>
</html>

the above code works fine if i run it in fiddle, but not when i run it on my local computer, can someone help me, what should i do for the above problem, any kind of help i thank you.

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

0

The <select id='purpose'> doesn't exist yet when you try to run $('#purpose').on('change', ...). Either move your <script> tag to the end of the <body> tag, or add a defer attribute to the tag (which follows best practices anyways).

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this, you just needed to move few things around.

$('#purpose').on('change', function() {
  if (this.value === "1") {
    $("#business").show();
    $("#local").hide();
  } else if (this.value === "2") {
    $("#business").hide();
    $("#local").show();
  } else {
    $("#business").hide();
    $("#local").hide();
  }
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #business {
      display: none;
    }
    
    #local {
      display: none;
    }
  </style>
</head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
  <select id='purpose'>
    <option value="0">Personal use</option>
    <option value="1">Business use</option>
    <option value="2">Passing on to a client</option>
  </select>
  <div id="business">
    <label for="business">Business Name</label>
    <input type='text' class='text' name='business' value size='20' />
  </div>
  <div id="local">
    <label for="local">Local Name</label>
    <input type='text' class='text' name='local' value size='20' />
  </div>
</body>

</html>

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