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

321
Visualizações
I need to add an alert (or similar popup) after hitting the "submit" button (with select & number options)

I am VERY knew to Javascript & HTML, just started studying IT and this is my first assessment. Essentially, the task is - provide a dropdown list and an age input box, provide a "Submit" button and add an alert box with JS that shows if the user has entered the correct details to proceed. (New Zealand and 18 (or higher) being the "correct" answers to proceed - other country options and lower than 18 get a rejection alert answer)

Here is my HTML so far:

<body>       
<form method="get">
 <select id="country" name="country">
 <option value="0">PLEASE SELECT YOUR COUNTRY</option>
 <option value="1">New Zealand</option>
 <option value="2">Australia</option>
 <option value="3">Switzerland</option>
 </select>  
<input type="number" id="dob">
</form> 
 
    <button type="button" id="submit" >SUBMIT</button>
   <script src="test.js"></script>
   </body> 

I have tried using Javascript (var as below) and also function(which I got confused with and deleted):

var select = document.getElementById("country");

document.getElementById("button").onclick = function(){
    var value = select.value
    if (value == "1")
    {
        alert("Thanks");
    }
    if (value == "2")
    {
        alert("sorry");
    }
    if (value == "3")
    {
        alert("sorry");
    }
    if (value == "0")
    {
        alert("sorry");
    }
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your id in document.getElementById("button").onclick is wrong. the id is submit not button

    var select = document.getElementById("country");

   document.getElementById("submit").onclick = function(){
var value = select.value
if (value == "1")
{
    alert("Thanks");
}
if (value == "2")
{
    alert("sorry");
}
    if (value == "3")
      {
    alert("sorry");
      }
    if (value == "0")
     {
        alert("sorry");
      }
    };

I've refactored your code. It should run like this

  const select = document.getElementById("country");

const inp = document.getElementById("dob");

document.getElementById("submit").onclick = function(){
  const selected = select.value;
  const dob = parseInt(inp.value);
  
  if( selected === "1" && dob >= 18 ){
    alert("Thanks");
  } else {
    alert("Sorry");
  }
};
<form method="get">
 <select id="country" name="country">
 <option value="0">PLEASE SELECT YOUR COUNTRY</option>
 <option value="1">New Zealand</option>
 <option value="2">Australia</option>
 <option value="3">Switzerland</option>
 </select>  
<input type="number" id="dob">
</form> 
 
    <button type="button" id="submit" >SUBMIT</button>

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