JQuery Range Input Listener - javascript

Hey having a little trouble jquery and the hmtl5 range. I'm try to get the values as they changed but the event listener is not capturing it. Currently my code is:
HTML
html += '<li>Apply Credits: <input type="range" min="0" max="'+credits+'" name="discount_credits" id="discount_credits" /> <span>'+credits+'</span></li>'
And the JS is:
$('#discount_credits').mousewheel( function() {
alert('it changed!');
alert('new value = ' + $(this).val());
});
I've also tried
$('#discount_credits').change( function() {
alert('it changed!');
alert('new value = ' + $(this).val());
});
Neither seem to work. Am I doing something wrong?

$('input[type=range]').on('input', function () {
$(this).trigger('change');
});
This fires the change event on every input event.

Does not seem to have any problem on HTML5 <input type="range"> using change.
See: http://jsfiddle.net/DerekL/BaEar/33/

I assume your span has an id: <span id="slidernumber">...</span>
Lets also assume that you have a few sliders, and you want to see a text change if any of them were moved:
<li>
Apply Credits1:
<input type="range" min="0" max="100"
name="discount_credits1" id="discount_credits"
/>
</li>
<li>
Apply Credits2:
<input type="range" min="0" max="100"
name="discount_credits2" id="discount_credits"
/>
</li>
<span id="slidernumber">50</span>
Try this:
$(document).ready(function(){
$("[type=range]").change(function(){
var newval=$(this).val();
$("#slidernumber").text(newval);
});
});
http://jsfiddle.net/MahshidZeinaly/CgQ5c/
Now lets assume that you have a few sliders, but you want to see a text change if a particular one of them were moved. (I assume it because the range input is inside li tag, and you gave it a name):
<li>
Apply Credits1:
<input type="range" min="0" max="100"
name="discount_credits1" id="discount_credits"
/>
<span id="slidernumber">50</span>
</li>
<li>
Apply Credits2:
<input type="range" min="0" max="100"
name="discount_credits2" id="discount_credits"
/>
<span id="slidernumber">50</span>
</li>
Try this:
$(document).ready(function(){
$("[type=range]").change(function(){
var newv=$(this).val();
$(this).next().text(newv);
});
});
http://jsfiddle.net/MahshidZeinaly/2pe7P/1/

The on change event seems to be working correctly for me: http://jsfiddle.net/zV3xD/7/
<input type="range" min="0" max="100" name="discount_credits" id="discount_credits" />
<span id="newValue" value="0">0</span>
$('#discount_credits').change( function() {
var newValue = this.value;
$('#newValue').html(newValue);
});​
Or you can try something like this:
<form oninput="result.value=parseInt(a.value)">
<input type="range" name="a" value="50" /> =
<output name="result">0</output>
</form>
Using the output example here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/output

Several test and bug fixed!
$('input[name=form_range]').change('mousestop',function () {
});

Related

get value of slider in typescript

I was trying to get the value of a slider in typescript as following:
My html:
<input id="width" type="range" value="250" min="50" max="500" (change)="getSliderValue()" />
my typescript code in my component.ts:
getSliderValue() {
var slider = document.querySelector("#width");
slider.addEventListener('change', () => {
var data = slider.value
});
}
however with this, i get following error on slider.value:
Property 'value' does not exist on type 'Element'
What am I doing wrong?
I explained pretty much everything in the comments, here is the code:
Html:
<input id="width" type="range" value="250" min="50" max="500" (change)="getSliderValue($event)" (oninput)="getSliderValue($event)" />
component.ts:
getSliderValue(event) {
console.log(event.target.value);
}
You can read this article or this question for a better understanding of the onchange and oninput events.
I'm follow Cristian and add ngModel binding to label show data
<label for="state">My range: <b> {{myValue}}%</b></label>
<input name="state" type="range" [(ngModel)]="myValue" (change)="getSliderValue($event)" (oninput)="getSliderValue($event)" id="formControlRange" value="0" min="0" max="100" />
and component.ts
myValue: any=0
getSliderValue(event:any=0) {
this.myValue = event.target.value
console.log(this.myValue );
}
you need to add oninput event as well for listing the slider value change event.
HTML
<input id="width" type="range" value="250" min="50" max="500" (change)="getSliderValue(this.value)" oninput="getSliderValue(this.value)" />
JS
<script>
function getSliderValue(value) {
console.log(value);
}
</script>

How to create another input range to Meteor event

I have this element in my HTML
<div class="slider-wrapper">
<input type="range" min="-10" max="100" value="1" step="0.01">
</div>
And my JS
'input input[type="range"]': function(event, template) {
var sliderVal = $( event.currentTarget ).val();
Session.set('scaler', sliderVal);
console.log("Slider: " + sliderVal);
}
I tried to change the class name of my element so I can add another input range slider mapped to a new event but it doesn't seem to work. How to add another one, changing input[type="range"] to scale[type="range"] and amend the element's class name won't work.
Template:
Each of your sliders is given a unique id. This id is what is going to tell one slider apart from the other.
<div class="slider-wrapper">
<input type="range" min="-10" max="100" id="slider1" value="1" step="0.01">
<input type="range" min="-10" max="100" id="slider2" value="1" step="0.01">
<input type="range" min="-10" max="100" id="slider3" value="1" step="0.01">
</div>
Event handlers:
A single event handler that does the job of picking up changes on any of the sliders.
// when anything in input[type="range"] changes,
'change input[type="range"]' (event){
console.log("id of the slider is: ", event.target.id);
console.log("it's current value is: ", event.target.value);
// set the id and it's value - which can be used later for whatever you want to do.
Session.set(event.target.id, event.target.value);
},
Session.get("slider1") ==> gets the slider1's current value
Session.get("slider2") ==> gets the slider2's current value
Session.get("slider3") ==> gets the slider3's current value
Okay, it seems that I was missing a hashtag to the event selector.
<div class="slider-wrapper">
<input id="scale" type="range" min="-1" max="1" value="1" step="0.01">
</div>
<div class="slider-wrapper">
<input id="offset" type="range" min="1" max="100" value="1" step="0.01">
</div>
'input #scale[type="range"]': function(event, template) {
var sliderVal = $( event.currentTarget ).val();
Session.set('scaler', sliderVal);
console.log("Slider: " + sliderVal);
},
'input #offset[type="range"]': function(event, template) {
var sliderVal = $( event.currentTarget ).val();
Session.set('offset', sliderVal);
console.log("Slider: " + sliderVal);
}

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

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>

Attaching an event listener to a specific input range value

Having an html input range values from 0 - 100 , how can i attach an event listener to specific values?
For example, when reaching values of 45, change the background color.
<input id="myrange" type="range" min="0" max="100" />
With jQuery
$("#myRange").change(onSlideChange);
function onSlideChange(){
var val = $("#myRange").val();
if(val==45){
$("#result").text("It's 45");
}else{
$("#result").text("It's another value ("+val+")");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="range" id="myRange" value="90">
<div id="result"></div>
Try this
Html
<input type="range" id="myRange" value="90" onchange="showVal(this.value)">
Js
function showVal(val){
if(val==45){
alert("It's 45");
}else{
alert("It's "+val);
}
}
Here, Instead of showing alert, you can do whatever you want :)

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