Created a simple Input and tried to set Max value using Jquery. Initially it sets the value to 77 and then decreases the max value to 50.
The below example works !
$(document).ready(function () {
minMaxAge("#test-input",0,77)
minMaxAge("#test-input",0,50) //Text Box max is now 50 // Working
});
function minMaxAge(id,min,max) {
$(id).change(function() {
if ($(this).val() > max)
{
$(this).val(max);
}
else if ($(this).val() < min)
{
$(this).val(min);
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="test-input" type="number" />
Now using the same example but increment the max value from 77 to 79.
The below example doesn't work.
$(document).ready(function () {
minMaxAge("#test-input",0,77)
minMaxAge("#test-input",0,79) //Text Box max is now 79 // Not Working
});
function minMaxAge(id,min,max) {
$(id).change(function() {
if ($(this).val() > max)
{
$(this).val(max);
}
else if ($(this).val() < min)
{
$(this).val(min);
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="test-input" type="number" /
I was Jstepper.Js library to achieve the result but had the similar problem. Works fine when decreasing the max value but doesn't work when increasing the max value.
Ref:Jstepper Library
Update:
Please check the updated snippet.
$(document).ready(function() {
$('input:radio[name="input-max"]').change(
function() {
if ($(this).is(':checked')) {
var min = 0;
var max = $(this).val();
minMaxAge("#test-input",0,max)
}
});
});
function minMaxAge(id, min, max) {
$(id).change(function() {
if ($(this).val() > max) {
$(this).val(max);
} else if ($(this).val() < min) {
$(this).val(min);
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
Have two Radio button and they have set the max number to be entered in the Input.
</p>
<input type="radio" id="input-max1" name="input-max" value=77>Max 77<br>
<input type="radio" id="input-max2" name="input-max" value=79> Max 79<br>
<input id="test-input" type="number" />
.change()adds an event handler without removing any previous handlers. By the time the handler that thinks the max is 79 is called, the previous handler has already reduced the value to 77.