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

166
Vistas
How to control the disability state of HTML inputs using Javascript

I have been trying to cycle between the disability states of two inputs on my website by using a button attached to some JavaScript. My current code has two buttons in use which both disable one input and enable the other, however this is not very practical. I wanted to know how I can use one button and one script to cycle between the disability states of my inputs, this is my current code:

<script>
function switch_monthlyexpense()
{
    document.getElementById("monthlyexpenses").disabled=false;
    document.getElementById("monthlypexpenses").disabled=true;

}

</script>

<script>
function switch_monthlypexpense()
{
    document.getElementById("monthlyexpenses").disabled=true;
    document.getElementById("monthlypexpenses").disabled=false;

}

</script>

<button class="btn btn-primary" onclick="switch_monthlyexpense()" id="monthlyexpensestoggle">Expenses</button>
<button class="btn btn-primary" onclick="switch_monthlypexpense()" id="monthlypexpensestoggle">P Expenses</button>


<form id="expenses_form" style="visibility:visible;">
   <div class="form-group">
   <div class="form-group col-md-1">
      <label for="monthlyexpenses" id="monthlyexpenseslabel">Monthly Expenses</label>
      <input type="number" class="form-control" id="monthlyexpenses" placeholder="0" disabled>
   </div>
       <span class="input-group-text" id="dsign5">$</span>
</form>



<form id="pexpenses_form" style="visibility:visible;">
   <div class="form-group">
   <div class="form-group col-md-1">
      <label for="monthlypexpenses" id="monthlypexpenseslabel">Monthly P Expenses</label>
      <input type="number" class="form-control" id="monthlypexpenses" placeholder="0">
   </div>
       <span class="input-group-text" id="psign4">%</span>
</form>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

disabled is an attribute so you can set it by:

document.getElementById("myId").setAttribute('disabled', '');

And to enable the element:

document.getElementById("myId").removeAttribute('disabled');

Your first function will look like:

function switch_monthlyexpense()
{
    document.getElementById("monthlyexpenses").removeAttribute('disabled');
    document.getElementById("monthlypexpenses").setAttribute('disabled', '');

}
about 4 years ago · Juan Pablo Isaza Denunciar

0

<script>
// use a variable to note which one is disabled
let selected = "monthlyexpenses"

function switchDisabled () {
    // toggle the from "monthlyexpenses" to "monthlypexpenses" or vice versa
    selected = selected === "monthlyexpenses" ? "monthlypexpenses" : "monthlyexpenses"
    
    // check if the DOM disabled state is the same as the selected
    document.getElementById("monthlyexpenses").disabled = selected === "monthlyexpenses";
    document.getElementById("monthlypexpenses").disabled = selected === "monthlypexpenses";

}

</script>

<button class="btn btn-primary" onclick="switchDisabled()" id="switch-button">Switch</button>

<form id="expenses_form" style="visibility:visible;">
   <div class="form-group">
   <div class="form-group col-md-1">
      <label for="monthlyexpenses" id="monthlyexpenseslabel">Monthly Expenses</label>
      <input type="number" class="form-control" id="monthlyexpenses" placeholder="0" disabled>
   </div>
       <span class="input-group-text" id="dsign5">$</span>
</form>



<form id="pexpenses_form" style="visibility:visible;">
   <div class="form-group">
   <div class="form-group col-md-1">
      <label for="monthlypexpenses" id="monthlypexpenseslabel">Monthly P Expenses</label>
      <input type="number" class="form-control" id="monthlypexpenses" placeholder="0">
   </div>
       <span class="input-group-text" id="psign4">%</span>
</form>
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