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

253
Views
Disable input in a form when another input is filled in JS

I am trying to disable all input type in a form when one of them is checked or filled with a value.

When one of the checkbox is checked will disable the other two fields, same for the input text.

Here some code try:

HTML

<form action="">
   <input type="text" id="name"/>
   <input type="checkbox" id="days" name="days" />
   <input type="checkbox" id="months" name="months" />
</form>

JS

 function disableInput(e) {
    if(e.target.value !== 0) {
       document.getElementById("days").disabled = true;
       document.getElementById("months").disabled = true;
     }else {
       document.getElementById("days").disabled = false;
       document.getElementById("months").disabled = false;
            }
        }

const name = document.querySelector('#name');
name.addEventListener('input', disableInput);

How can I manage this in vanilla JS?

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

0

The input event will bubble up. So listen for that event on the <form> element to capture all activity on the inputs.

When the event is triggered, check the type of event and if either the value is empty or the checked property equals true, depending on the type value.

Then loop over all the inputs in the form. All your inputs can be found in the elements property on the form. In the loop disable all the inputs except the one you're currently using.

Restore all the inputs when the aforementioned condition is not met.

const form = document.querySelector('form');

function disableInputs(exception) {
  for (const input of form.elements) {
    if (input !== exception) {
      input.disabled = true;
    }
  }
}

function enableInputs() {
  for (const input of form.elements) {
    input.disabled = false;
  }
}

form.addEventListener('input', event => {
  const { target } = event;

  if (
    (target.type === 'text' && target.value !== '') ||
    (target.type === 'checkbox' && target.checked === true)
  ) {
    disableInputs(target);
  } else {
    enableInputs();
  }
});
<form action="">
  <input type="text" id="name" />
  <input type="checkbox" id="days" name="days" />
  <input type="checkbox" id="months" name="months" />
</form>

about 4 years ago · Juan Pablo Isaza Report

0

function lockOut(e) {
  this.querySelectorAll("input:not(#" + e.srcElement.id + ")").forEach(function(elem) {
    if (e.srcElement.type !== "text") elem.disabled = !elem.disabled;
    else if (e.srcElement.value.length > 0) elem.disabled = true;
    else elem.disabled = false;
  });
}

document.querySelector("form").addEventListener("input", lockOut);
<form action="">
  <input type="text" id="name" />
  <input type="checkbox" id="days" name="days" />
  <input type="checkbox" id="months" name="months" />
</form>

about 4 years ago · Juan Pablo Isaza Report

0

This is my simple solution:

const allInputs = document.querySelectorAll('.radio-input');

allInputs.forEach((input) => {
  input.addEventListener('input', (e) => {
  
    let value;
    
    switch(e.target.type) {
      case 'checkbox':
        value = e.target.checked;
        break;
      case 'text':
        value = e.target.value;
        break;
    }
    
    const needDisable = !(value === '' || value === false);
    
    allInputs.forEach((input) => {
      if (input === e.target) {
        input.disabled = false;
        return;
      }
      
      input.disabled = needDisable;
    })
  });
});
<form action="">
   <input type="text" class="radio-input" id="name"/>
   <input type="checkbox" class="radio-input" id="days" name="days" />
   <input type="checkbox" class="radio-input" id="months" name="months" />
</form>

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!