How do I make jQuery UI slider snap to predefined values? - javascript

I have a slider in jQuery UI where I have some predefined values that the user can choose. However, right now, it just goes from 1-60 and not using my values. I have the following numbers:
1,3,5,15,30,60
This is my code:
$(document).ready(function(){
var valMap = [1,3,5,15,30,60];
$("#resolution-slider").slider({
min: 1,
max: 60,
values: [2],
slide: function(event, ui) {
$("#resolution").val(ui.values[0]);
$(".resolution-preview").html(ui.values[0]);
}
});
});
How can I make the slider snap to my values in valMap?

Why not do:
$(document).ready(function(){
var valMap = [1,3,5,15,30,60];
$("#resolution-slider").slider({
max: valMap.length - 1, // Set "max" attribute to array length
min: 0,
values: [2],
slide: function(event, ui) {
$("#resolution").val(valMap[ui.values[0]]); // Fetch selected value from array
$(".resolution-preview").html(valMap[ui.values[0]]);
}
});
});

I think that's what you want, have a try the codes below:
<div id="slide"></div>
var valMap = [1,3,5,15,30,60];
$("#slide").slider({
max: 60,
min: 1,
stop: function(event, ui) {
console.log(ui.value);
},
slide: function(event, ui) {
// 1,3,5,15,30,60
return $.inArray(ui.value, valMap) != -1;
}
});

Related

jQueryUI slider - How to get the value [duplicate]

I am working on http://gamercity.info/ymusic/.
And I am using UI slider as seek bar.
While the video is played I want to invoke a seekTo(seconds) function if user clicked anywhere on the seek bar. How to get new value of seconds after click event?
To read slider value/percentage value at anytime:
var val = $('#slider').slider("option", "value");
$('#slider').slider({
change: function(event, ui) {
alert(ui.value);
}
});​
http://jqueryui.com/demos/slider/
I checked all the answers mentioned above, but found none useful as the following code:
$('#slider').slider("values")[0]; // for slider with single knob or lower value of range
$('#slider').slider("values")[1]; // for highest value of range
Tested with jQuery v2.1.1 and jQuery-ui v1.12.1
var val = $("#slider").slider("value");
$("#slider").slider(
{
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#slider-value" ).html( ui.value );
}
}
);
JS FIDDLE EXAMPLE : http://jsfiddle.net/hiteshbhilai2010/5TTm4/1162/
you can have a function like this
function seekTo(seek_value)
{
$("#slider").slider('option', 'value',seek_value);
}
var value=document.getElementById('slider').value;
var a=value.split("specialName")//name=special charcter between minimum and maximum rang
var b=a[0];//this will get minimum range
var c=a[1];//this will get maximum range
// Price Slider
if ($('.price-slider').length > 0) {
$('.price-slider').slider({
min: 0,
max: 2000,
step: 10,
value: [0, 2000],
handle: "square",
});
}
$(document).ready(function(){
$(".price-slider").on( "slide", function( event, ui ) { console.log("LA RECTM"); var mm = $('.tooltip-inner').text(); console.log(mm); var divide = mm.split(':'); console.log( "min:" +divide[0] + " max:" + divide[1] ) } );
})
You can pass the value to any function or set any element with the value:
$(function () {
$('#slider').slider({
max: 100,
slide: function (event, ui) {
$('#anyDiv').val(ui.value);
}
});
});
Late to the party but this question has still unanswered.
Below example will show you how to get value on change in an input field to save in DB:
$( "#slider-videoTransparency" ).slider({
value: "1",
orientation: "horizontal",
range: "min",
max: 30,
animate: true,
change: function (e, ui) {
var value = $(this).slider( "value" );
$('.video_overlay_transparency').val(value);
}
});
<div id="slider-videoTransparency" class="slider-danger"></div>
<input type="hidden" name="video_overlay_transparency" class="video_overlay_transparency" value="">
JQuery ui slider
var slider = $("#range_slider").slider({
range: true,
min: 0,
max: 500,
values: [0, 500],
slide: function(event, ui) {
var x = ui.values[0];
var y = ui.values[1];
$('#min').text( ui.values[0] )
$('#max').text( ui.values[1] )
$.ajax({
url: '/search',
type: 'GET',
dataType: 'script',
data: {min:x, max:y,term:food_item_name},
success: function(repsonse){},
});
}
});
});

jQuery ui range slider gives wrong value

I use jQuery UI range slider for IE 9 and lower. With it I want to update the font size live.
My problem is that the range slider does not slide correctly. I cant get value 0 after 1. I also cant gate vaule 100 after 99.
function update() {
var Rvalue = $('#range-slider').slider('value');
$("#slidevalue").text(Rvalue);
};
$(function() {
$( "#range-slider" ).slider({
range:false,
min: 0,
max: 100,
value: 20,
slide: function( event, ui ) {
update();
}
});
});
Here is a jsFiddle
Your problem is your update function.
Try like this :
$(function() {
$( "#range-slider" ).slider({
min: 0,
max: 100,
value: 20,
slide: function( event, ui ) {
$( "#slidevalue" ).text( ui.value );
}
});
});

Change variable based on value of 'jQuery UI Slider'

I'm trying to update a variable based on the value of a jQuery UI Slider, which is then used within a function but I can't get it to work. Here's what I have so far:
$(document).ready(function() {
$('.drag').draggable();
$("#hue").slider({
min: -180,
max: 180,
value: 150,
slide: function(event, ui) {
$("#slideResult").text(ui.value);
},
change: function(event, ui) {
$("#slideResultSecret").val(ui.value);
}
});
var hueValue = document.getElementById("slideResultSecret").value;
$("#slideResult").text("null!");
});
$(window).load(function() {
$('.window img').pixastic("hsl", {hue: hueValue, rect:{left:115,top:115,width:490,height:692}});
});
Does that make sense? It's hard to explain it when you don't fully understand it yourself.
It is enough to define the variable inside the change event, like so:
change: function(event, ui) {
hueResult = $("#hue").slider("value");
}

How to get value from input with dynamically changed value by jquery UI slider?

I've input text with a dynamically changed value by jquery UI slider. How to get value from $("#scope_input") by jquery? .change event working only from manual keypress on keyboard, on sliding doesn't getting any value:
$("#scope_input").change(function() {
console.log($(this).val());
});
$("#scope_slider").slider({
range: "min",
min: 1,
max: 100,
value: 10,
slide: function(event, ui) {
$("#scope_input").val(ui.value);
}
});
You need to trigger the change event manually:
$("#scope_input").val(ui.value).change();
WHen you update the field within the slide event, trigger the change on the input
$("#scope_slider").slider({
range: "min",
min: 1,
max: 100,
value: 10,
slide: function(event, ui) {
$("#scope_input").val(ui.value).change();
}
});
Did you try
$("#scope_slider").bind("slidechange", function(event, ui) {
$("#scope_input").val(ui.value);
});

jQuery-ui slider - How to stop two sliders from controlling each other

This is in reference to the question previously asked
The problem here is, each slider controls the other. It results in feedback.
How do I possibly stop it?
$(function() {
$("#slider").slider({ slide: moveSlider2 });
$("#slider1").slider({ slide: moveSlider1 });
function moveSlider2( e, ui )
{
$('#slider1').slider( 'moveTo', Math.round(ui.value) );
}
function moveSlider1( e, ui )
{
$('#slider').slider( 'moveTo', Math.round(ui.value) );
}
});
This is sort of a hack, but works:
$(function () {
var slider = $("#slider");
var slider1 = $("#slider1");
var sliderHandle = $("#slider").find('.ui-slider-handle');
var slider1Handle = $("#slider1").find('.ui-slider-handle');
slider.slider({ slide: moveSlider1 });
slider1.slider({ slide: moveSlider });
function moveSlider( e, ui ) {
sliderHandle.css('left', slider1Handle.css('left'));
}
function moveSlider1( e, ui ) {
slider1Handle.css('left', sliderHandle.css('left'));
}
});
Basically, you avoid the feedback by manipulating the css directly, not firing the slide event.
You could store a var CurrentSlider = 'slider';
on mousedown on either of the sliders, you set the CurrentSlider value to that slider,
and in your moveSlider(...) method you check whether this is the CurrentSlider, if not, you don't propagate the sliding (avoiding the feedback)
You could just give an optional parameter to your moveSlider1 and moveSlider2 functions that, when set to a true value, suppresses the recursion.
A simpler approach which is kind of a hybrid of the above answers:
var s1 = true;
var s2 = true;
$('#slider').slider({
handle: '.slider_handle',
min: -100,
max: 100,
start: function(e, ui) {
},
stop: function(e, ui) {
},
slide: function(e, ui) {
if(s1)
{
s2 = false;
$('#slider1').slider("moveTo", ui.value);
s2 = true;
}
}
});
$("#slider1").slider({
min: -100,
max: 100,
start: function(e, ui) {
},
stop: function(e, ui) {
},
slide: function(e, ui) {
if(s2)
{
s1 = false;
$('#slider').slider("moveTo", ui.value);
s1 = true;
}
}
});
});
Tried this now and all answers do not work possibly due to changes to jquery ui.
The solution of Badri works if you replace
$('#slider').slider("moveTo", ui.value);
with
$('#slider').slider("option", "value", ui.value);

Categories

Resources