Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda