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

147
Visualizações
How to call JavaScript methods automatically anywhere in HTML?

My price input should be disabled or enabled automatically after selecting status. Now it turns in disabled after click on input (during wrong status) but it doesn't back to enabled when my status is good. What is the right way to do it ?

HTML:

 <div class="form-row">
   <div class="form-group col-md-6 text-center">
     <label for="cattery-status-create" th:text="#{cattery.status}"></label>
     <select id="cattery-status-create" class="form-control">
       <option th:each="status : ${catteryStatusCodeDict}" th:value="${status.code}" th:text="${status.value}"></option>
     </select>
   </div>
   <div class="form-group col-md-6 text-center">
     <label for="price-create" th:text="#{price}"></label>
     <input id="price-create" onclick="setPriceInputDisabled()"  type="number" class="form-control" max="10000" step="1">
   </div>
 </div>

JavaScript:

function setPriceInputDisabled(){
  document.getElementById("price-create").disabled = false;
  var catteryStatusCode = $("#cattery-status-create").find(":selected").val();

  if (catteryStatusCode != 'S') {
    document.getElementById("price-create").disabled = true;
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Unsure why you are mixing JavaScript an jQuery. I would use one or the other, not both.

function setPriceInputDisabled(){
  $("#price-create").prop("disabled", ($("#cattery-status-create").val() === "S"));
}

This will only enable the Input upon click event when Value is not "S".

The click event does not seem like the right callback to bind to. I suspect you want to modify the property when the Select element is changed. If the User selects a Value of "S"; the Input is disabled.

$(function() {
  function setPriceInputDisabled() {
    $("#price-create").prop("disabled", ($("#cattery-status-create").val() === "S"));
  }
  $("#cattery-status-create").change(setPriceInputDisabled);
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-row">
  <div class="form-group col-md-6 text-center">
    <label for="cattery-status-create" th:text="#{cattery.status}"></label>
    <select id="cattery-status-create" class="form-control">
      <option>A</option>
      <option>S</option>
      <option>D</option>
      <option>F</option>
      <option>G</option>
    </select>
  </div>
  <div class="form-group col-md-6 text-center">
    <label for="price-create" th:text="#{price}"></label>
    <input id="price-create" type="number" class="form-control" max="10000" step="1">
  </div>
</div>

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