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

231
Views
Check that after all inputs have value make button active

I want to remove the disabled attribute from the button when each field is filled out. My code works when a single input is filled. What am i doing wrong here?

Here are the HTML and JS

checkInput()

function checkInput() {
    let input = document.querySelectorAll('.form-control')
    const button = document.querySelector('.submit-btn')

    input.forEach(function (e) {
        let disabled = true;
        e.addEventListener('keyup', function () {
            if (e.value !== '') {
                disabled = false
            } else {
                disabled = true
                return false
            }

            if(disabled) {
                button.setAttribute('disabled', 'disabled')
            } else {
                button.removeAttribute('disabled')
            }
        })
    })
}
<div class="form-group">
  <label for="name">Name*</label>
  <input class="form-control" type="text" id="name" placeholder="Name">
</div>
<div class="form-group">
  <label for="lastName">lastName*</label>
  <input class="form-control" type="text" id="lastName" placeholder="lastName">
</div>
<div class="form-group">
  <label for="fiscalCode">fiscalCode*</label>
  <input class="form-control" type="text" id="fiscalCode" placeholder="fiscalCode">
</div>
<button type="submit" disabled="disabled" class="submit-btn">Continue</button>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It does not work because you want to have all inputs affect the state of a button, yet you only check one variable for adding/removing disabled property.

Here is a working code snippet with an example where i created an array of properties, one for each input, that i can refer to in every fired key up event

checkInput()

function checkInput() {
    let input = document.querySelectorAll('.form-control')
    const button = document.querySelector('.submit-btn')
    const disabled = Array(input.length).fill(true);

    input.forEach(function (e, index) {
        e.addEventListener('input', function () {
            disabled[index] = e.value === ''; // simplified if/else statement of yours

            if(disabled.some(Boolean)) {
                button.setAttribute('disabled', 'disabled')
            } else {
                button.removeAttribute('disabled')
            }
        })
    })
}
<div class="form-group">
  <label for="name">Name*</label>
  <input class="form-control" type="text" id="name" placeholder="Name">
</div>
<div class="form-group">
  <label for="lastName">lastName*</label>
  <input class="form-control" type="text" id="lastName" placeholder="lastName">
</div>
<div class="form-group">
  <label for="fiscalCode">fiscalCode*</label>
  <input class="form-control" type="text" id="fiscalCode" placeholder="fiscalCode">
</div>
<button type="submit" disabled="disabled" class="submit-btn">Prosegui</button>

Additionaly stoping the function execution when assigning disabled = true in the first else statement is also a wrong approach, as you most likely want to not only assign the disable value, but also the disabled property of the button.

EDIT: as mentioned in the comment by CID it is reasonable to change the event listener to input so we can handle the copying and pasting events as well

about 4 years ago · Juan Pablo Isaza Report

0

You are adding a keyup event for each input field. that event only checks the current input field if it is empty or not. it does not check the 3 input fields itself

this should do the trick:

checkInput()

function checkInput() {
    let input = document.querySelectorAll('.form-control')
    const button = document.querySelector('.submit-btn')

    input.forEach(function (e) {
        let disabled = true;
        e.addEventListener('keyup', function () {
           const emptyFields =  Array.from(input).filter( input => input.value === "");
           disabled = emptyFields.length > 0;
            if(disabled) {
                button.setAttribute('disabled', 'disabled')
            } else {
                button.removeAttribute('disabled')
            }
        })
    })
}
about 4 years ago · Juan Pablo Isaza Report

0

  1. You are enabling the submit button on keyup event of any 3 inputs. So it gets enabled on any 3 inputs.
  2. Remove the return on disabled flow to disable button after clearing.

checkInput()

function checkInput() {
    let input = document.querySelectorAll('.form-control')
    const button = document.querySelector('.submit-btn')

    input.forEach(function (e) {
        let disabled = true;
        e.addEventListener('keyup', function () {
            if (e.value !== '') {
                disabled = false
            } else {
                disabled = true
                // return false <-- this makes function exit before your disable button
            }
            if(disabled) {
                button.setAttribute('disabled', 'disabled')
            } else {
                button.removeAttribute('disabled')
            }
        })
    })
}
<div class="form-group">
  <label for="name">Name*</label>
  <input class="form-control" type="text" id="name" placeholder="Name">
</div>
<div class="form-group">
  <label for="lastName">lastName*</label>
  <input class="form-control" type="text" id="lastName" placeholder="lastName">
</div>
<div class="form-group">
  <label for="fiscalCode">fiscalCode*</label>
  <input class="form-control" type="text" id="fiscalCode" placeholder="fiscalCode">
</div>
<button type="submit" disabled="disabled" class="submit-btn">Continue</button>

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!