I'll try and keep this straightforward. I am using jQuery sliders and trying to initialize them all at once using an array:
sliders.push(new slider('paletteControl','pSlider',0,255,100,sliderChange()));
function slider (context, id, min, max, defaultvalue, changeFunc) {
this.context = context;
this.id = id;
this.min = min;
this.max = max;
this.defaultvalue = defaultvalue;
this.changeFunc = changeFunc;
}
Where context is the ID of its intended parent div and changeFunc is the function I want it to call on change.
My Init function loops through this array and appends the markup according to the context, and then tries to init each jquery slider like so:
$(id).slider({
range: "min",
min: sliders[i].min,
max: sliders[i].max,
value: sliders[i].defaultvalue,
create: function() {
handle.text( $( this ).slider( "value" ) );
},
change: function() {
sliders[i].changeFunc();
}
});
The min, max, and value inits work fine, presumably since they happen exactly once, during the init, but the change function fails pretty miserably, again presumably because it's simply trying to look up sliders[i] on each change (i being a long dead iterator by that point).
My question is - how can I programatically init a bunch of jquery sliders, each with a different onChange function? Without doing them manually, that is.
EDIT: Got some great help from Ramon de Paula Marques below, though in the end I had to do it a different way altogether because I was unable to pass a value to the function. What I ended up doing, for better or worse, was creating a wrapper function that simply looked up the proper change function once called based on the id of the slider that called it.
function parcelSliderFunction(caller, value)
{
for (var x = 0; x < sliders.length; x++)
{
if(sliders[x].id == caller) {
sliders[x].callback(value);
return;
}
}
console.log("id " + caller + " not found, you done screwed up.");
return;
}
I should probably use a dictionary for this.
First you have to remove parentheses of the function as parameter, in this line
sliders.push(new slider('paletteControl','pSlider',0,255,100,sliderChange()));
It becomes
sliders.push(new slider('paletteControl','pSlider',0,255,100,sliderChange));
Then you get the change function like this (without parentheses)
$(id).slider({
range: "min",
min: sliders[i].min,
max: sliders[i].max,
value: sliders[i].defaultvalue,
create: function() {
handle.text( $( this ).slider( "value" ) );
},
change: sliders[i].changeFunc
});
OR
$(id).slider({
range: "min",
min: sliders[i].min,
max: sliders[i].max,
value: sliders[i].defaultvalue,
create: function() {
handle.text( $( this ).slider( "value" ) );
}
});
$( id ).on( "slidechange", function( event, ui ) {} );
You may call the changeFunc using window[value.changeFunc] as shown below
function newSlider(context, id, min, max, defaultvalue, changeFunc) {
this.context = context;
this.id = id;
this.min = min;
this.max = max;
this.defaultvalue = defaultvalue;
this.changeFunc = changeFunc;
}
function func1() {
$('#output').append("func1<br>");
}
function func2() {
$('#output').append("func2<br>");
}
function func3() {
$('#output').append("func3<br>");
}
var sliders = [];
sliders.push(new newSlider('paletteControl', 'pSlider1', 0, 255, 100, "func1"));
sliders.push(new newSlider('paletteControl', 'pSlider2', 0, 255, 200, "func2"));
sliders.push(new newSlider('paletteControl', 'pSlider3', 0, 255, 70, "func3"));
$.each(sliders, function(index, value) {
$("#" + value.id).slider({
range: "min",
min: value.min,
max: value.max,
value: value.defaultvalue,
change: window[value.changeFunc]
});
});
Working code: https://plnkr.co/edit/nwjOrnwoI7NU3MY8jXYl?p=preview
Related
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){},
});
}
});
});
i create a js class and now i want to save a value of the slider (slider jquery UI) into textbox.
function Slider(name,orientation,range,disabled,min,max,value,valueLabel) {
this.name = name;
this.orientation = orientation;
this.range = range;
this.disabled = disabled;
this.min = min;
this.max = max;
this.value = value;
this.valueLabel = valueLabel;
this.setSlider = function() {
jQ(""+name).slider({
orientation: ''+orientation,
range: range,
min: min,
max: max,
value: value,
change: function(event, ui) {
jQ(valueLabel).attr('value', jQ(this).slider("value"));
}
});
}}
Can you help me? Why it's don't work?
valueLabel it's name of the label where i want to save value for example: #slider-value.
If i change a slider value i want get value of the slider. So i have to use change property.
You can write it like this, use slide instead of change
jQ(""+name).slider({
orientation: '' + orientation,
range: range,
min: min,
max: max,
value: value,
slide: function (event, ui) {
jQ(valueLabel).val(ui.value); // .val() works if it's a textbox
}
});
Your question is not quite clear but hope this will help you!
I am getting undefined type for:
$(".ui-slider-handle").attr("title", '"'+current+'"');
As you can see I tried to alert the current in line 29 and alerts the correct updated current value but it is not functioning on .attr("title", '"'+current+'"'). Why is this happening, and how can I solve this issue?
$(function () {
var current;
$("#slider-vertical").slider({
orientation: "vertical",
range: "min",
min: 0,
max: 100,
value: 60,
slide: function (event, ui) {
$("#amount").val(ui.value);
var offsets = $('.ui-slider-handle').offset();
var top = offsets.top;
$(".tooltip").css('top', top - 90 + "px");
$(".tooltip-inner").text(ui.value);
function setCurrent() {
current = ui.value;
}
setCurrent();
}
});
$("#amount").val($("#slider-vertical").slider("value"));
$(".ui-slider-handle").attr("rel", "tooltip");
$(".ui-slider-handle").attr("data-toggle", "tooltip");
$(".ui-slider-handle").attr("data-placement", "left");
// alert(current);
// $(".ui-slider-handle").attr("title", "50");
$(".ui-slider-handle").attr("title", '"'+current+'"');
$("[rel='tooltip']").tooltip();
});
What do you have the single-double-single quote for? Try to use:
$(".ui-slider-handle").attr("title", current);
Does it require double quotes to appear?
I'm gonna leave that cause it was boneheaded and funny. Anyway, current is inside the scope of the function and does not have a value outside of it. Try this instead:
Change this:
function setCurrent() {
current = ui.value;
}
setCurrent();
to
current = ui.value;
Or you could just return the value like this:
function setCurrent() {
return ui.value;
}
current = setCurrent();
I have written something like below. onclick of div with id "PLUS" I
am getting the following error:
cannot call methods on slider prior to initialization attempted to call method 'value'
<div id="PLUS" class="PLUS"></div>
<script>
$(function() {
$(".slider").slider({
animate: true,
range: "min",
value: 18,
min: 18,
max: 70,
step: 1,
slide: function(event, ui) {
$("#slider-result").html(ui.value);
document.getElementById(findElement('ageId')).value = ui.value;
},
//this updates the hidden form field so we can submit the data using a form
change: function(event, ui) {
$('#hidden').attr('value', ui.value);
}
});
$(".PLUS").click(function() {
var value = $("#slider-result").slider("value"),
step = $("#slider-result").slider("option", "step");
$("#slider-result").slider("value", value + step);
});
});
</script>
Any help is appreciated.
If we check error in detail you will notice that it says you are trying to call the value method before the initialization of slider plugin.
Reason:
Actually JavaScript is an interpreted language, and it doesn't wait for first command to execute and finish. That's why your $(".slider").slider({ and $(".PLUS").click(function() { lines run at same time and the error occurs.
Solution:
You can put your code in setTimeout function here is an example given below.
<script>
$(function() {
$(".slider").slider({
animate: true,
range: "min",
value: 18,
min: 18,
max: 70,
step: 1,
slide: function(event, ui) {
$("#slider-result").html(ui.value);
document.getElementById(findElement('ageId')).value = ui.value;
},
//this updates the hidden form field so we can submit the data using a form
change: function(event, ui) {
$('#hidden').attr('value', ui.value);
}
});
setTimeout(function(){
$(".PLUS").click(function() {
var value = $("#slider-result").slider("value"),
step = $("#slider-result").slider("option", "step");
$("#slider-result").slider("value", value + step);
});
},200); // 200 = 0.2 seconds = 200 miliseconds
});
</script>
I hope this will help you/someone.
Regards,
You have used $(".slider").slider() at the time of initializing and
$("#slider-result").slider() at the time of getting the value some plugins work on selector you have used at the time of init, so try that.
The error is caused because $("#slider-result") is not the element initialized as slider and you're trying to execute slider widget methods on it instead of $(".slider") which is the actual slider.
Your code should be
$(".PLUS").click(function() {
var value = $(".slider").slider("value"),
step = $(".slider").slider("option", "step");
$("#slider-result").text(value + step);
//---- maybe you'll need to ----^---- parseInt() the values here
});
i had a similar problem.
your block here
$(".PLUS").click(function() {
var value = $("#slider-result").slider("value")
, step = $("#slider-result").slider("option", "step");
$("#slider-result").slider("value", value + step);
});
just keep it under
create: function( event, ui ) {}
ie.
create: function( event, ui ) {
$(".PLUS").click(function() {
var value = ui.value;
, step = $("#slider-result").slider("option", "step");
$("#slider-result").slider("value", value + step);
});
}
hope this works.
The best way I found to achieve this is to use the event from the ON function from the slider library to get the value. Ex:
slider.on('slideStop', function(ev) {
let value= ev.value; //try ev if you want to see all
console.log(value);
})
Regards
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);