Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!