Trying to get a sliderbar to alter a textfield value in HTML - javascript

I have a sliderbar that takes input from the textfield and resets its position accordingly; however, I cannot figure out how to do the following:
Make it so that the sliderbar repositions itself for any value within the range, not just non-decimal inputs.
Make it so that the textfield value changes accordingly as the sliderbar is toggled.
The code can be found below.
Any help is appreciated, thank you!
<div>
<input id="slider" type="range" min="0" max="2" step="0.001"/>
<input id="input" type="text" value="1"/>
</div>
<script>
var slider = document.getElementById('slider');
$('#input').change(function(){
slider.value=parseInt(this.value)
});
</script>

I have a sliderbar that takes input from the textfield and resets its
position accordingly;
I don't see how that work with the code given since you're using parseInt and would need parseFloat for that. Anyway, below is what you asked for:
$('#input').change(function() {
$('#slider').val(parseFloat(this.value))
});
$('#slider').on("input change", function() {
$('#input').val(this.value)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div>
<input id="slider" type="range" min="0" max="2" step="0.001" />
<input id="input" type="text" value="1" />
</div>

You don't need to use parseInt() since both need to be string.
<div>
<input id="slider" type="range" min="0" max="2" step="0.001"/>
<input id="input" type="text" value="1"/>
</div>
<script>
var slider = document.getElementById('slider');
$('#input').change(function(){
slider.value=this.value
});
</script>

Related

Sync 2 HTML Inputs Range and Number

Hey I'm want to sync 2 inputs so if someone change the slider then should also change the number input and the other way round I know there are other answered Question but the did not work form me because it is important not to change any class names or ids only the number input sync with the slider but not also the other way round my code is:
<div class="slidecontainer">
<input type="range" min="0.25" max="5" value="1" step="0.25" class="slider" id="playbackSpeed">
<input type="number" value="1" class="number" id="playbackSpeed" placeholder="1,0">
</div>
my js is
document
.querySelector(".slider")
.addEventListener("input", updateValue);
document
.getElementsByClassName("number")
.addEventListener("input", updateValue);
function updateValue(e) {
var sibling =
e.target.previousElementSibling ||
e.target.nextElementSibling;
sibling.value = e.target.value;
}
You can even shorten the whole thing to:
const inputs=[...document.querySelectorAll("input")];
inputs.forEach((inp,i)=>inp.addEventListener("input",()=>
inputs[1-i].value=inputs[i].value )
)
<div class="slidecontainer">
<input type="range" min="0.25" max="5" value="1" step="0.25" class="slider" id="playbackSpeed">
<input type="number" value="1" class="number" id="playbackSpeed" placeholder="1,0">
</div>
let range=document.querySelector("input[type=range]");
let number=document.querySelector('input[type=number]')
range.addEventListener("change",(e)=>{
number.value=e.target.value;
})
number.addEventListener("change",(e)=>{
range.value=e.target.value;
})
For more goto: https://codepen.io/Gangster_sCode/pen/abNKJMm

using jquery and html form to show range value

I have included the form in my website with sliders using the range input html form. Though the range do not show the actual value by default I found a script to show the display for example "bedrooms" and "bathrooms".
P.S. When I submit the form using php the values are different. Which is alright.
Hoping that the display shows individual "range-value" for each bedroom and bathroom sections rather than showing same values while the sliders are moving. Perhaps giving a separate variable for both of them can lead to individual output? How can i do this? Can anyone help with it?
var range = $('.input-range'),
value = $('.range-value');
value.html(range.attr('value'));
range.on('input', function() {
value.html(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<form id="form" name="form" method="post" target="formfill" action="form-to-email.php">
<div class="field">
<label for="Bedrooms">Bedrooms</label>
</div>
<div class="range-slider">
<input class="input-range" type="range" value="1" min="0" max="10">
</div>
<span class="range-value"></span>
<div class="field">
<label for="bathrooms">Bathrooms</label>
</div>
<div class="range-slider">
<input class="input-range" type="range" value="1" min="0" max="10">
</div>
<span class="range-value"></span>
</form>
The problem is here:
range.on('input', function() {
// this sets the html content of all elements with class range-value
// to the value of the slider that is being moved
value.html(this.value);
});
The fix that I am showing simply moves the span tag into the div, this is just one way of knowing what .range-value belongs to witch slider.
Also since value="1" is hard coded into the range sliders, you can also just have the span tags start with the same value <span class="range-value">1</span>
$('.input-range').on('input', function() {
$(this).next('.range-value').html(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="range-slider">
<input class="input-range" type="range" value="1" min="0" max="10">
<span class="range-value">1</span>
</div>
<div class="range-slider">
<input class="input-range" type="range" value="1" min="0" max="10">
<span class="range-value">1</span>
</div>
I have removed the CSS and most of the HTML from the answer as it is not relevant to the problem, but to see it working in the full form here is a demo

Html/Js Input range object dynamically loaded inside a div don't works

Hi I have an hidden div with a range inside:
<div id="hiddencontainer" style="display:none;">
<input id="range1" type="range" min="1" max="10" step="1" name="rating" value="1" onmousemove="showrangevalue()"/>
</div>
This range works fine when it is displayed directly but I need to hidde this range and show it when user click on a specific button.
The problem is that after the user click on the "showing range" button, the range is displayed but impossible to read his value.
The demo and full code on jsbin:
http://jsbin.com/voyilovuya/3/
HTML
<div id="hiddencontainer" style="display:none;"><!-- try without display none -->
<input id="range1" type="range" min="1" max="10" step="1" name="rating" value="1" onmousemove="showrangevalue()"/>
</div>
<div id="container">
</div>
<div id="value">
</div>
<input type="button" value="Afficher" onClick="showhiddencontainer()"></input>
JS
function showrangevalue(){
document.getElementById("value").innerHTML=document.getElementById("range1").value;
}
function showhiddencontainer(){
document.getElementById("container").innerHTML=document.getElementById("hiddencontainer").innerHTML;
}
Have you a solution? Thanks for your help!
see this http://jsfiddle.net/g03srawu/1/,
you are duplicating the range element in container, so there are two range inputs with same ids,
This will show the values in the hidden element inside .value div. replace it with class, and then you can use it.
HTML:
<div id="hiddencontainer" style="display:none;"><!-- try without display none -->
<input class="range1" type="range" min="1" max="10" step="1" name="rating" value="1" onmousemove="showrangevalue()"/>
</div>
<div id="container">
</div>
value:
<div id="value">
</div>
<input type="button" value="Showing range" onClick="showhiddencontainer()"></input>
JS:
function showrangevalue(){
document.getElementById("value").innerHTML=document.querySelectorAll(".range1")[1].value;
}
function showhiddencontainer(){
document.getElementById("container").innerHTML=document.getElementById("hiddencontainer").innerHTML;
}
Try replacing
function showhiddencontainer(){
document.getElementById("container").innerHTML=
document.getElementById("hiddencontainer").innerHTML;
}
with
function showhiddencontainer(){
document.getElementById("hiddencontainer").style.display = "block";
}
unless you have a reason to copy your container over?

Dynamically change value of input textField

I have a html block as follows with an input textfield that updates the entire application when it is manually edited.
How can I dynamically target and change this input text field? In the following this would be value="2"
<my-attribute param="Count" min="0" max="100" step="1">
<span id="label">Count</span><span id="input">
<dat-input-number id="input" value="2" step="1" min="0" max="100">
<div id="blocker" on-dblclick="" style="width: 152px; height: 15px;"></div>
<input id="number" type="text" on-keydown="">
</dat-input-number></span>
</my-attribute>
<script>
document.getElementById('number').value = "yourValueHere";
</script>
Oh, and don't forget to close your <input id="number" type="text" on-keydown=""> with an </input>
<script>
var theInput = document.getElementById('input');
theInput.value = '5';
</script>
Working demo here
Note: This is super trivial and I have no doubt that you could have found enough knowledge through a Google search to attempt this on your own. :)

How to update html5 range on change of a text input?

I have a html5 range input and a text box input. When the range changes, the text box will get updated. But what should I do so that when I put a number in the text box, the range input gets updated?
This is my current code:
<div>
<input id="slider" type="range" min="0" max="200" />
<input id="box" type="text" value="0"/>
</div>
<script>
var slider = document.getElementById('slider');
$('#box').change(function(){slider.value=parseInt(this.value)});
</script>
When I edit my text box, my slider (range input) does NOT change. What am I doing wrong? Can it be that I shouldn't be using slider.value to update the slider? If that is the reason, what is the correct way to do that?
Hey Everyone Those who don't know , we don't need any js or jquery for this
<form>
<div>
<input id="rangeInput" type="range" min="0" max="200" oninput="amount.value=rangeInput.value" />
<input id="amount" type="number" value="100" min="0" max="200" oninput="rangeInput.value=amount.value" />
</div>
</form>
It's a simple script, just do this:
<label for=range1>Slide, then press "Click to search slider value"</label>
<span>-1000 </span><input type="range" id="slide1" min="-1000" max="1000" value=0 step=0.25 onchange="printValue('slide1', 'rangeValue1')"><span> 1000 </span>
<i><input type=text id="rangeValue1" value=0 width=25% onchange="slideValue('slide1', 'rangeValue1');"><span> is the value of the slider now</span></i><br/><br/>
<script>
function printValue(sliderID, spanbox) {
var x = document.getElementById(spanbox);
var y = document.getElementById(sliderID);
x.value = y.value;
}
function slideValue(sliderID, spanbox){
var x = document.getElementById(spanbox);
var y = document.getElementById(sliderID);
y.value = parseInt(x.value);
}
window.onload = function() { printValue('slide1', 'rangeValue1'); }
</script>
(Of course, this only works when you press enter and submit the value to the "onchange")
in my side I did it in just Javascript and adding a onchange event.
I also added the value=0 to range so that make it start at the beguining.
<div>
<input id="slider" type="range" min="0" max="200" value="0"/>
<input id="box" type="text" value="0"/>
</div>
​​​​​​​
var slider = document.getElementById('slider');
var box = document.getElementById("box");
slider.onchange = function(){
box.value = slider.value;
}
And add this if you want to make it work on both side
box.onkeyup = function(){
slider.value = box.value;
} ​
This fiddle works:
var slider = document.getElementById('slider');
$('#box').change(function(){
slider.value=parseInt(this.value);
});
(So that's your current code, actually. Did you import the jQuery library?)
Keep in mind that you've set the slider's range to 200. Try some values between 0 and 200.
It's actually quite easy. It's all written in jQuery Tools API documentation. So I changed my code to this and it worked:
<div>
<input id="slider" type="range" min="0" max="200" />
<input id="box" type="text" value="0"/>
</div>
<script>
var sliderapi = $("#slider").data("rangeinput");
$('#box').change(function(){sliderapi.setValue(parseInt(this.value))});
</script>
$('#box').change(function(){
$('#slider').attr({"value":parseInt(this.value)});
});
Might work.

Categories

Resources