Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!