1

I need to set the values of the slider when it is created, how can I do it? Example:

<div class="slider" data-min-value="0"  data-max-value="120"><input type="range"></div>





$('.slider').slider({
            range: true,
            create: function(event, ui) {
                console.log($(this).data('minValue'));
                ui.values[0] = $(this).data('minValue');
                ui.values[1] = $(this).data('maxValue');
            },
})

But ui is empty.

1
  • Why would you have a range input inside a jQuery UI slider ? Commented Apr 12, 2015 at 15:22

2 Answers 2

1

The ui.values array is just an argument, changing it doesn't change the settings of the slider.

You change the settings like this

$('.slider').slider({
    range: true,
    create: function (event, ui) {
        $(this).slider( "option", "max", $(this).data('maxValue') );
        $(this).slider( "option", "min", $(this).data('minValue') );
    }
});

Note that you'd usually just do this on instantiation

$('.slider').each(function() {
    $(this).slider({
        max: $(this).data('maxValue'),
        min: $(this).data('minValue')
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

For data attributes accessed using the .data() method, do not use camelCase:

ui.values[0] = $(this).data('min-value');
ui.values[1] = $(this).data('max-value');

Only use camelCase when you're using the native .dataset object, i.e.:

ui.values[0] = $(this)[0].dataset.minValue;
ui.values[1] = $(this)[0].dataset.maxValue;

Comments

Your Answer

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