I have a field in which when activating checkebox, it disables the input

Asked

Viewed 40 times

1

I have a field in which to activate the checkebox, he disables the input, but when I reload the page checkebox is disabled, so having to turn it back on.

function desabilitar(selecionado) {
    document.getElementById('tel').disabled = selecionado;
}
<div class="form-group">
  <label for="NomeEscola" style="font-weight: normal">Telefone Residencial:
      <input type="checkbox" onclick="desabilitar(this.checked)"/> O mesmo do aluno</label>
  <div class="input-group">
      <div class="input-group-addon" style="background-color: #FAFAFA">
          <i class="fa fa-arrow-circle-right" aria-hidden="true"></i>
      </div>
      <input type="text" class="form-control" name="TelefoneResidencial" id="tel" data-inputmask="'alias': '(99)9999-9999'" maxlength="150" />
  </div>
</div>

  • You want that when loading the page the checkbox is already marked and the field disabled?

  • So if I mark it, then reload the page then it should remain marked, the same unlike

  • You can do it using localStorage: https://developer.mozilla.org/en-US/docs/Web/API/Window/Window.localStorage

  • and failed to complete his question?

  • Another thing, it disables 1 imput, as it would when you check the checkbox, disable more than one?

1 answer

0


Can use a localStorage to save the value of the parameter selecionado. Then you check if it exists and is equal to "true" and trigger a click on the checkbox. But put a id at the checkbox to know exactly where the click will be fired:

<input id="mesmoaluno" type="checkbox" onclick="desabilitar(this.checked)"/>
       ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

Code:

function desabilitar(selecionado) {
   document.getElementById('tel').disabled = selecionado;
   localStorage.setItem("checked", selecionado); // cria o localStorage
}

// aguarda o DOM ser carregado
document.addEventListener("DOMContentLoaded", function(){
   // verifica se o localStorage existe e é igual a "true"
   if(localStorage.getItem("checked") === "true"){
      document.getElementById("mesmoaluno").click(); // dispara o click no checkbox
   }
});

I didn’t put a working snippet in here because he won’t take it localStorage, but in this JSFIDDLE it is possible to test. Just check/uncheck the checkbox and click on "RUN" again.

  • And another thing, it disables 1 imput, as it would for when you check the checkbox, disable more than one?

  • Just put inside the function the inputs you want to disable, as you did with the document.getElementById('tel').disabled = selecionado;.

  • Be sure to mark an answer to your questions with (only one answer).

  • excuse how I mark the question?

  • See also this answer that you didn’t score =]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.