0

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>

2
  • Remove available var you do not need that - just add this in attr -> max: +total - +sold Commented Feb 11, 2022 at 1:58
  • like this -> $(document).ready(function() { var total = $(".total").html(); var sold = $(".sold").html(); $(".qty").attr({ max: +total - +sold }); }); works the same way but less code is better always. Commented Feb 11, 2022 at 2:00

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.