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

235
Visualizações
Disable and enable button by over or under age 18 Javascript

Im new to JavaScript.

I struggled to find solution of enable and disable button using by if statement of under and over age 18, first click vote button was enable when I insert number 10 on textbox then disable button is stuck and cant change to enable after change the number of over age 18.

        //age verification

            if(Age < 18)
            {
            document.getElementById('age').style.borderColor='#e52213';
            document.getElementById("Btn").disabled = true;
            }
            if(Age > 18)
            {
                document.getElementById("Btn").disabled = disabled;
            }

Check image of vote button stuck on webpage at below:

enter image description here

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

0

You can assign false to the disabled attribute. Also, as your code stands, you can omit the condition simply using the else block.

Demo:

var ageEl = document.getElementById('age');

manageBtn(ageEl);

ageEl.addEventListener('input', function(){manageBtn(ageEl);});

function manageBtn(el){
  var age = ageEl.value;
  if(age < 18){
    document.getElementById('age').style.borderColor='#e52213';
    document.getElementById("Btn").disabled = true;
  }
  else{
    document.getElementById('age').style.borderColor='';
    document.getElementById("Btn").disabled = false;
  }
}
<input type="number" id="age">
<button id ="Btn">My Button</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

The disabled property is a Boolean so something like

btn = document.getElementById("Btn")

if(Age <= 18)
{
    document.getElementById('age').style.borderColor='#e52213';
    btn.disabled = true;
}
else if(Age > 18)
{
    btn.disabled = false;
}

also a couple of other things, you should use else if instead of another if so you don't check for something you already know is false,

use <= for the case where the age is 18

and also probably better imo to save the btn to a variable so you dont fetch the element twice.

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