I have just an input tag with the following logic:
https://codepen.io/ion-ciorba/pen/MWVWpmR
I have a minimum value coming from the database(400 in this case), the logic is good but the user interaction with the component is really bad, the user can’t input a value that is below 400, I want something else that won’t block the user from typing, maybe some other type of interaction besides change and input. How can I make this interaction more user friendly, but still maintain the minimum value at 400.
Maybe a better solution for that:
JavaScript
x
6
1
if (numberInputValue == "" || parseInt(numberInputValue) < parseInt(min)) {
2
numberInputValue = min;
3
} else if (parseInt(numberInputValue) > parseInt(max)) {
4
numberInputValue = max;
5
}
6
Advertisement
Answer
I agree with @Twisty the jQuery UI Slider would be better suited
JavaScript
1
43
43
1
$(function() {
2
let slider = $(".tbi-slider")[0];
3
let loanAmount = $(".tbi-calc-loanAmount");
4
let totalLoanAmount = $(".tbi-calc-loanTotalAmount");
5
var min = $(".tbi-slider").attr("min");
6
var max = $(".tbi-slider").attr("max");
7
$("#co-tbi-loanAmount-input").change(function(s) {
8
var numberInputValue = s.target.value;
9
if (numberInputValue.match(/^[0-9]+$/) == null) {
10
$("#co-tbi-loanAmount-input").val(min);
11
}
12
slider.value = parseInt(numberInputValue);
13
if (parseInt(numberInputValue) < parseInt(min)) {
14
$("#co-tbi-loanAmount-input").val(min);
15
} else if (parseInt(numberInputValue) > parseInt(max)) {
16
$("#co-tbi-loanAmount-input").val(max);
17
}
18
if (
19
parseInt(numberInputValue) >= parseInt(min) &&
20
parseInt(numberInputValue) <= parseInt(max)
21
) {
22
$("#co-tbi-loanAmount-input").val(numberInputValue);
23
}
24
$("#tbi-range-slider").slider("value", $(this).val());
25
});
26
27
$("#tbi-range-slider").slider({
28
min: 400,
29
max: 1000,
30
orientation: "horizontal",
31
range: "min",
32
33
slide: function(event, ui) {
34
refreshSwatch(),
35
$("#co-tbi-loanAmount-input").val(ui.value);
36
},
37
38
});
39
});
40
41
function refreshSwatch() {
42
$("#tbi-range-slider").css("background-color", "#729fcf");
43
}
JavaScript
1
17
17
1
body {
2
font-family: system-ui;
3
background: #f06d06;
4
color: white;
5
text-align: center;
6
}
7
8
#tbi-range-slider {
9
display: inline-block;
10
width: 300px;
11
margin: 15px;
12
background-image: none;
13
}
14
15
#tbi-range-slider .ui-slider-range {
16
background: #ef2929;
17
}
JavaScript
1
11
11
1
<input id="co-tbi-loanAmount-input" class="tbi-calc-loanAmount co-tbi-input tbi-font-18" value="400">
2
3
4
<div class="tbi-slider" id="tbi-range-slider" min="400" max="1000" value="500"></div>
5
6
7
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
8
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
9
10
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
11
<link rel="stylesheet" href="/resources/demos/style.css">
I hope this helps