I am making a website where I want to use range slider(I know it only supports webkit browsers).
I have integrated it fully and works fine. But I would like to use a textbox
to show the current slide value.
I mean if initially the slider is at value 5, so in text box it should show as 5, when I slide the value in text box should change.
Can I do this using only CSS
or html
. I want to avoid JQuery
. Is it possible?
Try This :
<input min="0" max="100" id="when_change_range" type="range">
<input type="text" id="text_for_show_range">
and in jQuery section :
$('#when_change_range').change(function(){
document.getElementById('text_for_show_range').value=$(this).val();
});
If you want your current value to be displayed beneath the slider and moving along with it, try this:
<!DOCTYPE html>_x000D_
<html>_x000D_
<head>_x000D_
<meta charset="utf-8">_x000D_
<title>MySliderValue</title>_x000D_
_x000D_
</head>_x000D_
<body>_x000D_
<h1>MySliderValue</h1>_x000D_
_x000D_
<div style="position:relative; margin:auto; width:90%">_x000D_
<span style="position:absolute; color:red; border:1px solid blue; min-width:100px;">_x000D_
<span id="myValue"></span>_x000D_
</span>_x000D_
<input type="range" id="myRange" max="1000" min="0" style="width:80%"> _x000D_
</div>_x000D_
_x000D_
<script type="text/javascript" charset="utf-8">_x000D_
var myRange = document.querySelector('#myRange');_x000D_
var myValue = document.querySelector('#myValue');_x000D_
var myUnits = 'myUnits';_x000D_
var off = myRange.offsetWidth / (parseInt(myRange.max) - parseInt(myRange.min));_x000D_
var px = ((myRange.valueAsNumber - parseInt(myRange.min)) * off) - (myValue.offsetParent.offsetWidth / 2);_x000D_
_x000D_
myValue.parentElement.style.left = px + 'px';_x000D_
myValue.parentElement.style.top = myRange.offsetHeight + 'px';_x000D_
myValue.innerHTML = myRange.value + ' ' + myUnits;_x000D_
_x000D_
myRange.oninput =function(){_x000D_
let px = ((myRange.valueAsNumber - parseInt(myRange.min)) * off) - (myValue.offsetWidth / 2);_x000D_
myValue.innerHTML = myRange.value + ' ' + myUnits;_x000D_
myValue.parentElement.style.left = px + 'px';_x000D_
};_x000D_
</script>_x000D_
_x000D_
</body>_x000D_
</html>
_x000D_
Note that this type of HTML input element has one hidden feature, such as you can move the slider with left/right/down/up arrow keys when the element has focus on it. The same with Home/End/PageDown/PageUp keys.
I have a solution that involves (Vanilla) JavaScript, but only as a library. You habe to include it once and then all you need to do is set the appropriate source
attribute of the number inputs.
The source
attribute should be the querySelectorAll
selector of the range input you want to listen to.
It even works with selectcs. And it works with multiple listeners. And it works in the other direction: change the number input and the range input will adjust. And it will work on elements added later onto the page (check https://codepen.io/HerrSerker/pen/JzaVQg for that)
Tested in Chrome, Firefox, Edge and IE11
;(function(){_x000D_
_x000D_
function emit(target, name) {_x000D_
var event_x000D_
if (document.createEvent) {_x000D_
event = document.createEvent("HTMLEvents");_x000D_
event.initEvent(name, true, true);_x000D_
} else {_x000D_
event = document.createEventObject();_x000D_
event.eventType = name;_x000D_
}_x000D_
_x000D_
event.eventName = name;_x000D_
_x000D_
if (document.createEvent) {_x000D_
target.dispatchEvent(event);_x000D_
} else {_x000D_
target.fireEvent("on" + event.eventType, event);_x000D_
} _x000D_
}_x000D_
_x000D_
var outputsSelector = "input[type=number][source],select[source]";_x000D_
_x000D_
function onChange(e) {_x000D_
var outputs = document.querySelectorAll(outputsSelector)_x000D_
for (var index = 0; index < outputs.length; index++) {_x000D_
var item = outputs[index]_x000D_
var source = document.querySelector(item.getAttribute('source'));_x000D_
if (source) {_x000D_
if (item === e.target) {_x000D_
source.value = item.value_x000D_
emit(source, 'input')_x000D_
emit(source, 'change')_x000D_
}_x000D_
_x000D_
if (source === e.target) {_x000D_
item.value = source.value_x000D_
}_x000D_
}_x000D_
}_x000D_
}_x000D_
_x000D_
document.addEventListener('change', onChange)_x000D_
document.addEventListener('input', onChange)_x000D_
}());
_x000D_
<div id="div">_x000D_
<input name="example" type="range" max="2250000" min="-200000" value="0" step="50000">_x000D_
<input id="example-value" type="number" max="2250000" min="-200000" value="0" step="50000" source="[name=example]">_x000D_
<br>_x000D_
_x000D_
<input name="example2" type="range" max="2240000" min="-160000" value="0" step="50000">_x000D_
<input type="number" max="2240000" min="-160000" value="0" step="50000" source="[name=example2]">_x000D_
<input type="number" max="2240000" min="-160000" value="0" step="50000" source="[name=example2]">_x000D_
<br>_x000D_
_x000D_
<input name="example3" type="range" max="20" min="0" value="10" step="1">_x000D_
<select source="[name=example3]">_x000D_
<option value="0">0</option>_x000D_
<option value="1">1</option>_x000D_
<option value="2">2</option>_x000D_
<option value="3">3</option>_x000D_
<option value="4">4</option>_x000D_
<option value="5">5</option>_x000D_
<option value="6">6</option>_x000D_
<option value="7">7</option>_x000D_
<option value="8">8</option>_x000D_
<option value="9">9</option>_x000D_
<option value="10">10</option>_x000D_
<option value="11">11</option>_x000D_
<option value="12">12</option>_x000D_
<option value="13">13</option>_x000D_
<option value="14">14</option>_x000D_
<option value="15">15</option>_x000D_
<option value="16">16</option>_x000D_
<option value="17">17</option>_x000D_
<option value="18">18</option>_x000D_
<option value="19">19</option>_x000D_
<option value="20">20</option>_x000D_
</select>_x000D_
<br>_x000D_
_x000D_
</div>_x000D_
<br>
_x000D_
For people don't care about jquery use, here is a short way without using any id
<label> userAvatar :
<input type="range" name="userAvatar" min="1" max="100" value="1"
onchange="$('~ output', this).val(value)"
oninput="$('~ output', this).val(value)">
<output>1</output>
</label>
version with editable input:
<form>
<input type="range" name="amountRange" min="0" max="20" value="0" oninput="this.form.amountInput.value=this.value" />
<input type="number" name="amountInput" min="0" max="20" value="0" oninput="this.form.amountRange.value=this.value" />
</form>
If you're using multiple slides, and you can use jQuery, you can do the follow to deal with multiple sliders easily:
function updateRangeInput(elem) {_x000D_
$(elem).next().val($(elem).val());_x000D_
}
_x000D_
input { padding: 8px; border: 1px solid #ddd; color: #555; display: block; }_x000D_
input[type=text] { width: 100px; }_x000D_
input[type=range] { width: 400px; }
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
_x000D_
<input type="range" min="0" max="100" oninput="updateRangeInput(this)" value="0">_x000D_
<input type="text" value="0">_x000D_
_x000D_
<input type="range" min="0" max="100" oninput="updateRangeInput(this)" value="50">_x000D_
<input type="text" value="50">
_x000D_
Also, by using oninput
on the <input type='range'>
you'll receive events while dragging the range.
Shortest version without form
, min
or external JavaScript.
<input type="range" value="0" max="10" oninput="num.value = this.value">
<output id="num">0</output>
_x000D_
Explanation
If you wanna retrieve the value from the output
you commonly use an id
that can be linked from the oninput
instead of using this.nextElementSibling.value
(we take advantage of something that we are already using)
Compare the example above with this valid but a little more complex and long answer:
<input id="num" type="range" value="0" max="100" oninput="this.nextElementSibling.value = this.value">
<output>0</output>
With the shortest answer:
this
, something weird in JS for newcomersinput
placing the id
in the output
Notes
min
value when equal to
0
this
keyword makes it a better languagean even better way would be to catch the input event on the input itself rather than on the whole form (performance wise) :
<input type="range" id="rangeInput" name="rangeInput" min="0" max="20" value="0"
oninput="amount.value=rangeInput.value">
<output id="amount" name="amount" for="rangeInput">0</output>
Here's a fiddle (with the id
added as per Ryan's comment).
<form name="registrationForm">_x000D_
<input type="range" name="ageInputName" id="ageInputId" value="24" min="1" max="10" onchange="getvalor(this.value);" oninput="ageOutputId.value = ageInputId.value">_x000D_
<input type="text" name="ageOutputName" id="ageOutputId"></input>_x000D_
</form>
_x000D_
if you still looking for the answer you can use input type="number" in place of type="range" min max work if it set in that order:
1-name
2-maxlength
3-size
4-min
5-max
just copy it
<input name="X" maxlength="3" size="2" min="1" max="100" type="number" />
For those who are still searching for a solution without a separate javascript code. There is little easy solution without writing a javascript or jquery function:
<input type="range" value="24" min="1" max="100" oninput="this.nextElementSibling.value = this.value">
<output>24</output>
_x000D_
If you want to show the value in text box, simply change output to input.
Update:
It is still Javascript written within your html, you can replace the bindings with below JS code:
document.registrationForm.ageInputId.oninput = function(){
document.registrationForm.ageOutputId.value = document.registrationForm.ageInputId.value;
}
Either use element's Id or name, both are supported in morden browsers.
Source: Stackoverflow.com