Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda