Search code examples
jqueryjquery-uijquery-slider

jQuery UI Slider to change percentage when select or input is changed


I am having problems updating the ui jquery slider when a select is changed or input is changed - anyone have any ideas on the source to resolve this?

$('.application-progress').slider({
    range: "min",
    min: 0,
    max: 100,
    animate: true,
    slide: function(event, ui){
        $("#progress").html(ui.value+"%");
    }
});
$("#progress").html($(".application-progress").slider("value")+"%");

$('input').focusout(function(){
    var percentage = 0;
    $('input').each(function(){
        if($(this).val() != "")
            percentage += 10;
    });
    $("#progress").slider({value: percentage});
    $("#progress").html($(".application-progress").slider("value")+"%");
});​

I have added to jsfiddle: http://jsfiddle.net/Ykx5f/1/.


Solution

  • Is that you are trying to achieve?

    $("input, select").change(function() {
        var percentage = 0;
        $('input, select').each(function() {
            if ($.trim(this.value) != "") percentage += 10;
        });
        $(".application-progress").slider("value", percentage);
    
        $("#progress").html(percentage + "%");
    });​
    

    DEMO: http://jsfiddle.net/Ykx5f/9/


    And here is the updated version, which automatically calculates the percentage based on the number of input fields:

    $("input, select").change(function() {
        var fields = $("input, select");
        var percentage = 100 * fields.filter(function() {
            return $.trim(this.value) != "";
        }).length / fields.length;
    
        $(".application-progress").slider("value", percentage);
        $("#progress").html(percentage + "%");
    });​
    

    DEMO: http://jsfiddle.net/Ykx5f/11/