12

I'm trying to generalize some slider code so that it will work with a few input + slider combinations.

I have everything essentially working - slide the slider, the input value updates or enter a number in the input, the slider moves to the proper location.

However, I'm having trouble initializing the sliders from the default values of the inputs. Here's what my code looks like:

Javascript

$(".slider").slider({
        min: 0,
        max: 5,
        value: $(this).parent().find(".inputNumber").val(),
        step: 0.25,
        slide: function (event, ui) {
            $(this).parent().find(".inputNumber").val(ui.value);
        }
    });

HTML

    <div class="inputWrap hidden">
    <input class="mini inputNumber" type="text" value="0.5">
    <div class="slider"></div>
    </div>
0

2 Answers 2

24

This works for me (with any number of sliders):

$(".slider").slider({
    min: 0,
    max: 5,
    step: 0.25,
    slide: function (event, ui) {
        $(this).parent().find(".inputNumber").val(ui.value);
    },
    create: function(event, ui){
        $(this).slider('value',$(this).parent().find(".inputNumber").val());
    }
});

Demo: http://jsfiddle.net/darkajax/uv5aC/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - didn't know about the create option. Big help.
Works for any number of sliders unless those sliders are from Good Times... Then red death.
3

this seems to work

$(".slider").each(function(){
    $(this).slider({
    min: 0,
    max: 5,
    step: 0.25,
    slide: function (event, ui) {
        $(this).parent().find(".inputNumber").val(ui.value);
    }
}).slider( "option", "value", $(this).parent().find(".inputNumber").val() );
});

FIDDLE

2 Comments

but what if he has multiple sliders and multiple inputNumber?
I do in fact need this to work for more than one slider, which this answer unfortunately, does not: jsfiddle.net/hte2B/1

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.