Search code examples
jqueryjquery-uiuislider

Jquery ui slider: value, min and max in the html


I am making some sliders with jquery-ui and i have a question how i can do the follow:

I have this html code:

<div class="slider range-slide">
    <b>A range slider:</b>
    <span class="amount"></span>
    <div class="slide"></div> 
</div> 

And this is the js:

$(".range-slide").each(function () {
    var $this = $(this);
    $(".slide", $this).slider({
        values: [30, 60],
        min: 0,
        max: 100,
        range: true,
        slide: function (event, ui) {
            $("span.amount", $this).html("" + ui.values[0] + 
                                         " - " + ui.values[1]);
        }
    });
    $(".range-slide span.amount").html("" + $(".slide").slider("values", 0) + 
                                       " - " + $(".slide").slider("values", 1));
});   

Everyting is working fine but how can i do someting like this:

<div class="slider range-slide">
    <b>A range slider:</b>
    <span class="amount"></span>
    <div class="slide" value="30,60" max="100" min="10"></div> 
</div> 

Solution

  • Change your slider to use those values.

    $(".slide", $this).slider({
        values: [30, 60],
        min: $(this).attr('min'),
        max: $(this).attr('max'),
        range: true,
        slide: function(event, ui) {
            $("span.amount", $this).html("" + ui.values[0] + " - " + ui.values[1]);
        }
    });
    

    UPDATED: Check out this fiddle

    $(".range-slide div.slide").each(function() {
    $(this).slider({
        values: [30, 60],
        min: parseInt($(this).attr('min')),
        max:  parseInt($(this).attr('max')),
        range: true,
        slide: function(event, ui) {
            $("span.amount", $(this).parent()).html("" + ui.values[0] + " - " + ui.values[1]);
        }
    });
    $(".range-slide span.amount").html("" + $(this).slider("values", 0) + " - " + $(this).slider("values", 1));
    });
    

    UPDATE 2 Use split to separate the values by commas and assign them to variables. Fiddle

    $(".range-slide div.slide").each(function() {
        values = $(this).attr('value').split(',');
       firstVal = values[0];
        secondVal = values[1];
    
    
        $(this).slider({
            values: [firstVal , secondVal],
            min: parseInt($(this).attr('min')),
            max:  parseInt($(this).attr('max')),
            range: true,
            slide: function(event, ui) {
                $("span.amount", $(this).parent()).html("" + ui.values[0] + " - " + ui.values[1]);
            }
        });
        $(".range-slide span.amount").html("" + $(this).slider("values", 0) + " - " + $(this).slider("values", 1));
        });