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

205
Visualizações
Remove error text when input is unlocked in jquery

I have a form with a select, input and a function that locks my input when an option is selected.

I added a warning that inputs are locked when someone selects an option. I would like to add a function to remove this warning when someone chooses an option with value="".

It's removing my warning but for example when I choose option text 1 then text 2 my warning displays twice and then when I choose a selection with first option it removes warning but only first.

How to change it so that the warning displays only once, and not more times, and removes it after select with option first.

$(function() {
  $('#stato').change(function() {
    var value = $(this).val(); //Pobranie wartości z tego selecta
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
      $("#error").remove();
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#data_consegna').after('<div id="error" style="color:red;">Input locked</div>');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
</div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The simple way to achieve what you require is to have the notification div always contained in the DOM, but hidden, and then hide/show it depending on the state of the select, like this:

jQuery(function($) {
  $('#stato').change(function() {
    var value = $(this).val();
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
      $("#error").hide();
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#error').show();
    }
  });
});
#error {
  color: red;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
  <div id="error">Input locked</div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

my warning displays twice

You don't check if it's already there or remove it.

removes only first

IDs must be unique, so $("#error").remove(); will only remove the first one. Use classes instead of IDs to remove multiple elements. If you only add once, this would not be an issue; just explaining why it removes only the first.

As noted in the other answer, the best solution is to simply .show()/.hide(), so I won't repeat that here.

To update your code, you can always remove the error, then add it back if needed - this isn't the most efficient as noted above.

Updated snippet:

$(function() {
  $('#stato').change(function() {
    var value = $(this).val(); //Pobranie wartości z tego selecta
    
    // always remove any existing error
    $("#error").remove();
    
    if (value == "") {
      $('#data_consegna').prop('disabled', false);
    } else {
      $('#data_consegna').prop('disabled', true);
      $('#data_consegna').after('<div id="error" style="color:red;">Input locked</div>');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div class="col-4">
  <label>Enabled </label>
  <select name="stato" id="stato" class="form-control">
    <option value="">Choose</option>
    <option value="Text1">Text1</option>
    <option value="Text2">Text2</option>
    <option value="Text3">Text3</option>
    <option value="Text4">Text4</option>
  </select>
</div>
<div class="col-4">
  <label>Disabled when choose option in select</label>
  <input id="data_consegna" type="text" class="form-control" name="data_consegna" placeholder="Data Consegna" />
</div>

Also note, as it's using an ID, it can only be used for a single error message.

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