Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jQuery – Set input range max to variable

I want to set my max on the input to be the difference between .total and .sold. Why is the max attribute not being set on the .qty input?

$(document).ready(function() {
  var total = $(".total").html();
  var sold = $(".sold").html();
  var available = $(".available").html(total - sold);
  $(".qty").attr({
    max: available
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
sold<span class="sold">3</span> total
<span class="total">10</span> available
<span class="available"></span>
<input type="range" min="1" value="1" class="qty" name='quantity' oninput="num.value = this.value">
<output id="num">0</output>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s because var available = $(".available").html(total - sold); returns a jQuery collection not a number. Try this

$(document).ready(function() {
  var total = $(".total").html();
  var sold = $(".sold").html();
  var available = total - sold; // now available is a number
  $(".available").html(available);
  $(".qty").attr({
    max: available
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
sold<span class="sold">3</span> total
<span class="total">10</span> available
<span class="available"></span>
<input type="range" min="1" value="1" class="qty" name='quantity' oninput="num.value = this.value">
<output id="num">0</output>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use