value is not set in slider html control - javascript

About Range input type in HTML
When I change value in slider (Range input type) that value in display in textbox 
But when I also same thing in change value in textbox it’s not set that value in slider (Range input type).
Here is code
Function for it:
we also want that when we slide the slider as well as value will change in textbox too
function :
<script type="text/javascript">
function updateTextInput(val) {
document.getElementById('textInput').value=val;
}
function updateTextInput2(val) {
document.getElementById('range1').value = val;
}
</script>
HTML :
<input type="range" min="0" max="100" value="0" id="range1" onchange="updateTextInput(this.value);"/>
<input type="text" id="textInput" value="0" onchange="updateTextInput2(this.value);">

Debug the updateTextInput function to verify the value to set,
or try
document.getElementById('textInput').value = document.getElementById('range1').value;

You need to change the event where the function is called in the textbox from onchange to oninput. The onchange event only fires after the control loses focus, i.e. when the user clicks on something else. The oninput event gets fired every time the user changes the value in the textbox.
Your code should look like this:
<input type="range" min="0" max="100" value="0" id="range1" onchange="updateTextInput(this.value);" />
<input type="text" id="textInput" value="0" oninput="updateTextInput2(this.value);">
Your JavaScript functions work 100% fine.
Here is a working example on codepen.

Related

Formatting input while values are typed

I'm trying to format the values typed by the user in realtime, I'm doing this for some data types but the only one that I cannot make it work is with currency (number). Based on my other data types I'm trying to solve it with the following code:
<input type="number" onkeypress="return parseFloat(this.value).toFixed(2).toLocaleString() " />
<input type="number" onkeypress="something(this.value);" />
function something(val) {
return parseFloat(val).toFixed(2).toLocaleString();
}
function something(val) {
return parseFloat(val).toFixed(2).toLocaleString();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" onkeypress="return parseFloat(this.value).toFixed(2).toLocaleString() " />
<input type="number" onkeypress="something(this.value);" />
What I'm trying to is that for example: if my user is trying to type 25999.84125 change the input value to 25999.84 but, any of my code examples are working.
How can I solve this? I'll prefer to keep the event + function inside the input tag (example 1) but if the solution is writing a custom function it's okay. Also I'm open to use Regex.
For using changing the value real-time, don't return the data in the function, just set the value to the input !
Also you cannot do it exactly real-time with JS, you need to use onChange function like this:
function something(val, input) {
document.getElementById(`input${input}`).value = parseFloat(val).toFixed(2).toLocaleString();
}
<input id="input1" type="number" onChange="something(this.value, 1);" />
<input id="input2" type="number" onChange="something(this.value, 2);" />
finally, if you want to do it exactly real time, use Vue js

Disabling keyboard use for input range

I have a standard form with a slider:
`<input type="range" min="0" max="100" value="50" id="my_slider" name="my_slider">`
The value of the slider can be selected either with the mouse or with the arrow keys (after clicking on the slider). How can I disable the keyboard and force the user to position the slider with the mouse?
One possibility is obviously to disable the arrow keys on the whole page with Java Script, but I would like to avoid that if possible.
Thank you.
You can block the keys with onkeydown event...
<input type="range" min="0" max="100" value="50" id="my_slider" name="my_slider"
onkeydown="event.preventDefault()">
You can add a key listener to the input and call Event.preventDefault() on the event which will cancel it, preventing the default action from being triggered:
var mySlider = document.getElementById("my_slider");
mySlider.addEventListener("keydown", function(event) {
event.preventDefault();
return false;
});
<input type="range" min="0" max="100" value="50" id="my_slider" name="my_slider">

How to set slider step values to some fixed values?

I'm working on sliders with the jQuery sliders, I have a inout slider as
<form id="sliderData">
<input id="slider1" type="range" min="0" max="50" step="">
</form>
I want the slider to set up to only some fixed values as [2,30,20,30,35,45]. So I cannot set up the step value. I have tried to fire the event by this code
$('form#sliderData').change(function () {
alert("Hello");
});
This event is not even firing.Is there anyway to set slider to random values
The event is not being fired because you need to hook the change event to the slider control itself, not it's parent form.
The problem you have is that you cannot specify specific steps in a slider control. They must all be the same step apart from the min to the max values. To solve this you could instead make the values of the slider match the indexes of the values in your JS array, and have a label showing the actual option chosen. Something like this:
var values = [2, 30, 20, 30, 35, 45];
$('#slider1').on('input', e => $('span').text(values[e.target.value]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="sliderData">
<input id="slider1" type="range" min="0" max="5" value="0">
<span>2</span>
</form>

onchanged event, get value prior to changing? in html input tag (type=range)

Is there a way to extract the value of an input tag prior to input change at the onchange event? Or is there an event triggered before 'change' instead?
In particular I need it for type range.
This question adds to previously asked similar questions because I also
ask if there is NO way to 'catch' the value before it is being changed as
in many other programming languages.
<form >
<input type="range" value="0" id="indentA" name="indentA" min="-20" max="20" onchange="alert('trying to output original value here... \ncurrent value is: '+this.value);" >
</form>
Is this the one you are looking?
I am capturing the old value when the slider changes.
Please have a look at the below code snippet.
Code Snippet
var previos_value=0;
function getValue(value){
alert('current value is: '+value);
alert('Previous value is: '+previos_value);
previos_value=value;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<form >
<input type="range" value="0" id="indentA" name="indentA" min="-20" max="20" onchange="getValue(this.value)" >
</form>

How to dynamically update range slider

I've created the following code to show the user their range slider value. However, it only shows the value when the user stops moving the slider. Is there a way to show the value WHILE the user drags the range slider? I'm looking for a way to do this in vanilla JS.
function updateInput(val) {
document.getElementById('textInput').innerHTML=val;
}
<input type="range" name="rangeInput" min="0" max="100" onchange="updateInput(this.value);">
<p id="textInput"></p>
Here you go:
<input type="range" name="rangeInput" min="0" max="100" onchange="updateInput(this.value);" oninput="updateInput(this.value)" >
<p id="textInput"></p>
oninput is not supported in IE10, so you have to use both, oninput and onchange.
Here is the demo
Use oninput instead of onchange.
Magical proof!
onmousemove function make this happen:
<input type="range" name="rangeInput" min="0" max="100" onmousemove="document.getElementById('textInput').innerHTML=this.value;">
<p id="textInput"></p>

Categories