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

148
Vistas
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 Respuestas
Responde la pregunta

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 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