jQuery: (this) ui.value solution - javascript

I'm pretty new to jquery, but this is question. From my code below I have made 3 sliders each within a tag. Each of the three has an input field that is changed when the slider is moved to show the current value of the slider (simple jQueryUI plugin sliders). The final line involves another plugin that links all the sliders together so that they all add to 100 total and move when the others move.
The problem I'm having is that when I slide one, its value changes and shows but the others do not. I can get all of the values to change with some extra lines of code, but I don't know how to get (this) ui.value so that I can display the separate values for each of the sliders.
You can see in the third slider I tried to get it to do that, but the way it is set it only takes the same ui.value of the current slider being moved.
Can anyone help me with a solution to get (this) ui.value of the other sliders when i need to change it?
($(function() {
$( "#RedSlider" ).slider({
orientation: "horizontal",
range: "min",
min: 0,
max: 100,
value: 1,
animate: true,
slide: function( event, ui ) {
$( "#RedValue" ).val( ui.value );}
});
$( "#RedValue" ).val( $( "#RedSlider" ).slider( "value" ) );
});
$(function() {
$( "#BlueSlider" ).slider({
orientation: "horizontal",
range: "min",
min: 0,
max: 100,
value: 1,
animate: true,
slide: function( event, ui ) {
$( "#BlueValue" ).val( ui.value );}
});
$( "#BlueValue" ).val( $( "#BlueSlider" ).slider( "value" ) );
});
$(function()
{
$( "#EmptySlider" ).slider({
orientation: "horizontal",
range: "min",
min: 0,
max: 100,
value: 98,
animate: true,
slide: function( event, ui )
{
$( "#EmptyValue" ).val( ui.value );
$( "#RedValue").val(ui.value);
$( "#BlueValue").val(ui.value);
}
});
$( "#EmptyValue" ).val( $( "#EmptySlider" ).slider( "value" ) );
$( "#RedValue").val($("RedSlider)").slider("value"));
$( "#BlueValue").val($("RedSlider)").slider("value"));
});*/
$('div:gt(2)').slider().linkedSliders({policy: 'last'});

Your code will only every be executed once. You need to change the following line:
$( "#BlueValue" ).val( $( "#BlueSlider" ).slider( "value" ) );
To be something different when selecting the value of your slider at whatever time you need it.

This code:
$( "#BlueValue" ).val( $( "#BlueSlider" ).slider( "value" ) );
will only execute once. You need to have that in a value-changed callback for the slider, so the value will be updated continually.

Related

jQuery Slider Min Value

I know there is a min value in jquery's slider. However, is there a way I can make the slider handle stop moving under some value? Is there any variable I can change to make this happen?
Note: My Slider is vertical
Basically, if the minimum is 20, I don't want the slider handle going under 20.
Well you can check it in slide functionality as below:
DEMO HERE
$(function() {
$( "#slider-range" ).slider({
orientation: "vertical",
range: false,
value:20,
slide: function( event, ui ) {
if (ui.value < 20) // corrected
return false;
else
$( "#amount" ).val( ui.value );
}
});
$( "#amount" ).val( $( "#slider-range" ).slider( "value" ) );
});

Adding dynamic elements

I am using the code bellow to use the Jquery UI Slider.
<script>
$(function() {
$( "#slider" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.value );
}
});
$( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) );
});
</script>
How can add multiple sliders with a button? I want to add slider elements when I click the button bellow.
<div id="adding"></div>
And how can I control them? This single slider has a jQuery code to work...
$("#adding").click(function() {
$("<div>").slider({
// options
}).appendTo("#sliderContainer");
});

jQuery Change slider values respectively

I create 3 slider, [Slider 1, Slider 2, Slider 3], and they work fine individually. but I want to make them depend on each other and work respectively to main slider.
You can see my demo here : JSFIDDLE
Main slider is Slider 1, and other slider 2 and slider 3 are depend on slider 1.
If slider 1's value increase (when we slide slider) then slider 2's and slider 3's value should increase too. and same as with when we decrease value.
Second point : If we set slider 1 to 100 and then slider 2 and 3 also on value of 100 (Automatically), then 100 is min. value of slider 2 and 3. in short value of slider 1 is minimum value of slider 2 and 3.
For Example is slider 1 set to 500 then slider 2 and 3 can slide to increase to more then 500 but can not decrease lower then 500.
here is a sample of my code
<div id="slider1"></div><br>
<div id="slider2"></div><br>
<div id="slider3"></div><br>
Slider 1 : <div id="value1"></div><br>
Slider 2 : <div id="value2"></div><br>
Slider 3 : <div id="value3"></div><br>
jQuery
$(function() {
$( "#slider1" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#value1" ).text(ui.value);
$( "#slider2" ).slider('option',{min: ui.value});
$( "#slider2" ).slider('value',ui.value);
$( "#slider3" ).slider('option',{min: ui.value});
}
});
});
$(function() {
$( "#slider2" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#value2" ).text(ui.value);
$( "#value2" ).val( "$" + $( "#slider2" ).slider( "value" ) );
$( "#amount" ).val( "$" + ui.value );
}
});
});
$(function() {
$( "#slider3" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#value3" ).text(ui.value);
$( "#value3" ).val( "$" + $( "#slider3" ).slider( "value" ) );
$( "#amount" ).val( "$" + ui.value );
}
});
});
You shouldn't use min option from slider.
Use return false on slide event when the value will be lower than slider1
Example in jsfiddle
Full code:
$(function() {
$( "#slider1" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#value1" ).text(ui.value);
$('#value2').text(ui.value); // slider 2 text
$('#value3').text(ui.value); // slider 3 text
$( "#slider2" ).slider('value',ui.value);
$('#slider3').slider('value',ui.value);
//$( "#slider2" ).slider('option',{min: ui.value}); remove this
//$( "#slider3" ).slider('option',{min: ui.value});
}
});
$( "#slider2" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
if(ui.value < $('#slider1').slider('value')){ // if the value is lower than slider 1
return false; // don't slide
}
$( "#value2" ).text(ui.value);
$( "#value2" ).val( "$" + $( "#slider2" ).slider( "value" ) );
$( "#amount" ).val( "$" + ui.value );
}
});
$( "#slider3" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
if(ui.value < $('#slider1').slider('value')){ // if the value is lower than slider 1
return false; // don't slide
}
$( "#value3" ).text(ui.value);
$( "#value3" ).val( "$" + $( "#slider3" ).slider( "value" ) );
$( "#amount" ).val( "$" + ui.value );
}
});
});
I made some change to your code.
Basically, setting the min value will not alwas change the slider position, you are juste setting the value when the slider is all left. In the example the slider 2 set it's min value depending on the current slider1's value. The slider 3 is setting it's min showed value the depending on the current slider1's value.
You can optimize a little bit but I think this is what you want to achieve !
//min value of slider 2 = current value of slider 1
$( "#slider2" ).slider('option',{min: ui.value});
//slider 3 cannot be under slider 1
if($( "#slider3" ).slider("value") < ui.value)
$( "#slider3" ).slider('option',{value: ui.value});
Check it here
it's a bit messy...anyway i hope this could help you out:
$(function() {
$( "#slider1" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#value1" ).text(ui.value);
if(ui.value%100 == 0)
{
$( "#slider2" ).slider('option',0);
$( "#slider2" ).slider('value',0);
$( "#slider3" ).slider('option',0);
}
else{
$( "#slider2" ).slider('option',{min: ui.value});
$( "#slider2" ).slider('value',ui.value);
$( "#slider3" ).slider('option',{min: ui.value});
}
}
});
});
just to highlight an important point, I have just realized it accepts ONLY Integer values on this command:
$( "#slider2" ).slider('value',IntValue);
So, if you needed to change its value by passing input text, then you must cast it this way:
parseInt($("#input_text_slider2").val())
so you are able to put that value using the variable IntValue above.

detect textbox change when the jquery UI slider moves

I have this fiddle http://jsfiddle.net/48Hpa/ , it calculates the sum automatically when I enter the keyword by myself, when I change the slider, the textbox value changes, but I don't see the sum. Why is this? How can I make this work? Thank you very much.
$(document).ready(function(){
$(function() {
$( "#slider1" ).slider({
range: "min",
value: 36,
min: 1,
max: 36,
slide: function( event, ui ) {
$( ".amount1" ).val( ui.value);
}
});
$( ".amount1" ).val($( "#slider1" ).slider( "value" ));
});
$(function() {
$( "#slider2" ).slider({
range: "min",
value: 50000,
min: 1,
max: 50000,
slide: function( event, ui ) {
$( ".amount2" ).val( ui.value );
}
});
$( ".amount2" ).val( $( "#slider2" ).slider( "value" ));
});
function writesum() {
var months = $('.amount1').val();
var credits = $('.amount2').val();
var payment = Number(months) + Number(credits);
$('.amount3').val(payment);
}
$(".amount1").on('input',function(){
jQuery(writesum);
});
$(".amount2").on('input',function(){
jQuery(writesum);
});
});
How about just calling writesum() whenever the slider moves?
i.e. http://jsfiddle.net/48Hpa/1/
You need to trigger a change event on the input fields when you change their value with code.
Also, I suggest using the change event.
$( "#slider1" ).slider({
...
slide: function( event, ui ) {
$( ".amount1" ).val( ui.value)
.trigger('change');
}
});
$( "#slider2" ).slider({
...
slide: function( event, ui ) {
$( ".amount2" ).val( ui.value )
.trigger('change');
}
...
$(".amount1, .amount2").on('change', function() {
writesum();
});

JQuery Slider: Calling function without ID but with the object

I'm implementing a Slider with JQuery and I have the following problem.
The function is called upon load and it generates a slider with the Id from the div-element:
$(function() {
$( "#slider" ).slider({
value: 0,
min: 0,
max: 10,
step: 1,
slide: function( event, ui ) {
$( "#value" ).val(ui.value );
}
});
$( "#value" ).val( $( "#slider" ).slider( "value" ) );
});
<div id="slider" >
I would rather have it called upon loading and pass the object -something like this:
function createSilder(object) {
object.slider({
value: 0,
min: 0,
max: 10,
step: 1,
slide: function( event, ui ) {
$( "#value" ).val(ui.value );
}
});
$( "#value" ).val( $( "#slider" ).slider( "value" ) );
}
<div id="slider" onload="createSlider(this)">
Is this possible?
Have you tried your code?
There is probably no problem, it just depends on the way the plugin has been coded.
Just be careful to pass the jQuery object : <div id="slider" onload="createSlider($(this));">.

Categories