retrieve jquery slider value in js - javascript

i have a jquery slider function and other javascript functions
i would like to retrieve the value of the jquery slider in javascript but i cant
if i do this
var s=$( "#slider_placement" ).slider( "value" );
in javascript it says error
Error: cannot call methods on slider prior to initialization; attempted to call method 'value'
i tried doing this in java script
var h = $('#custom-handle').text();
console.log(h);
but the console shows me empty result
here is the jquery slider code
$( function() {
var handle = $( "#custom-handle_wins" );
$( "#slider_wins" ).slider({
min:1,
max:10,
value:1,
step:1,
create: function() {
handle.text( $( this ).slider( "value" ) );
},
slide: function( event, ui ) {
handle.text( ui.value );
var s=ui.value;
document.getElementById("no_wins").innerHTML='Number of Wins: ' + ui.value;
}
});
s=$( "#slider_wins" ).slider( "value" );
document.getElementById("no_wins").innerHTML='Number of Wins: ' + s;
} );

Related

Pass jQuery slider variables through to Laravel controller

Currently, I am using the jQuery slider with range. What I want to do is, upon the user submitting the form, I want the two values (max and min) to also be sent over to the controller, so I can save them in the database. They are both currently saved in the variable values:
$(function() {
$( "#slider-range" ).slider({
range: true,
min: 0,
max: 1000000,
step: 10000,
values: [100000 , 300000 ],
slide: function( event, ui ) {
$( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ] );
},
stop: function(event, ui) {
var values = $( "#slider-range" ).slider( "values" );
}
});
$( "#amount" ).val( "£" + $( "#slider-range" ).slider( "values", 0 ) +
" - £" + $( "#slider-range" ).slider( "values", 1 ) );
});
I have alerted the variable and it echos the correct two variables. I am at a complete loss as to how to get the two variables, possibly split, over to my Laravel controller, once the user submits the form.
I usually get the form's variables through the Input::get method:
$id = Input::get('id');
But trying to access this variable always returns NULL
Any help would be hugely appreciated.
Here is a jsfiddle of how to get it working properly. My guess is that you forgot to set the "name" attribute on the HTML input field.

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");
});

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: (this) ui.value solution

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.

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