3

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.

2 Answers 2

3

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"));
    }
});
Sign up to request clarification or add additional context in comments.

Comments

3

Add this to your ready function:

// display slider value on load
$("#slider-result1").html($("#slider1").slider("value"));
$("#slider-result2").html($("#slider2").slider("value"));

// perform calculations on load
calPrice($("#slider1").slider("value"));
calDays($("#slider2").slider("value"));

http://jsfiddle.net/samliew/cC3Qh/1/

1 Comment

Thanks, its works, but I went with Yuriy Rozhovetskiy answer :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.