Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Slider - Using default value in function

I need some help with jQuery UI slider, I have set the initial value but I can't seem to find a way to display it when the page loads. I also need to be able to call both slider values to a function to do a calculation, then display it to the screen.

You can view my Fiddle test here > http://jsfiddle.net/cC3Qh/

jQuery

$(function() {
    $( "#slider1").slider({ 
        max: 2500,
        value: 1000,
        slide: function( event, ui ) { $( "#slider-result1" ).html( ui.value ); },
        change: function(event, ui) { calPrice(ui.value); }
    });

    $( "#slider2").slider({ 
        max: 30,
        value: 12,
        slide: function( event, ui ) { $( "#slider-result2" ).html( ui.value ); },
        change: function(event, ui) { calDays(ui.value); }
    });
  });

  function calDays(sliderval){
    var day = sliderval;
    day = day * 100;
    document.getElementById('price2').innerHTML = day;
    var price = document.getElementById('price1').innerHTML;
    price = price * day;
    document.getElementById('price3').innerHTML = price;
  }

  function calPrice(sliderval){
    var price = sliderval;
    price = price * 100;
    document.getElementById('price1').innerHTML = price;
  }

Thanks.

like image 344
grandpagohan Avatar asked Jul 20 '26 07:07

grandpagohan


1 Answers

Add create event handlers to sliders:

$("#slider1").slider({
    max: 2500,
    value: 1000,
    slide: function (event, ui) {
        $("#slider-result1").html(ui.value);
    },
    change: function (event, ui) {
        calPrice(ui.value);
    },
    create: function (event, ui) {
        $("#slider-result1").html($(this).slider("value"));
    }
});
like image 52
Yuriy Rozhovetskiy Avatar answered Jul 22 '26 21:07

Yuriy Rozhovetskiy