Im trying to write a code that will take a set variable from a text field (integer) and if the number is greater than or equal to 10000 then set a new variable to 10000 if its less than or equal to set the new variable to the same value as the other variable.
var sc = $('#Single-num1').val(); // Water Amount input
console.log(sc);
This is the first variable (sc).
This is what I am trying to get working and the logic makes sense but it wont give me data.
if (sc >= 10000) {
var ssa = 10000;
}
else {
var ssa = sc;
}
Just incase you need the HTML as well.
<div class="singlewateramountdiv">
<label for="Single-Water-Amount">How Much Water Was Used?</label>
<input type="text" id="Single-num1" name="Single-num1"/>
</div>
Sorry I know this seems super simple but can't seem to get it working.
CodePudding user response:
Convert the string to a number and do a simple check
var sc = Number($('#Single-num1').val());
var ssa = sc;
if (sc > 10000) ssa = 10000;
with a ternary
var sc = Number($('#Single-num1').val());
var ssa = (sc > 10000) ? 10000 : sc;
with Math.min
var sc = Number($('#Single-num1').val());
var ssa = Math.min(sc, 10000);