tags:

views:

25

answers:

1

Using the jQuery UI Slider, I'm trying to figure out how to make it so that the slider stops working once certain conditions are met. Any ideas? I thought stopping event propogation in the "start" part would work, but ...it doesn't. So I'm still clueless and lost.

<script type="text/javascript">
    $(document).ready(function () {
        var spendable = 1000;
        var spent = 0;

        function spend(quantity) {

            var remaining = spendable - quantity;
            $('#spendable').text(remaining);
        }

        $("#eq .slider").each(function () {
            var current = 0;
            $(this).slider({
                range: "min",
                step: 100,
                value: 0,
                min: 0,
                max: 500,
                animate: true,
                orientation: "horizontal",
                start: function (event, ui) {
                    if (spent < spendable)
                        return true;
                    event.stopPropagation();
                },
                slide: function (event, ui) {
                    // set the current value to whatever is selected.
                    current = ui.value;
                    $(this).parent('div:eq(0)').find('.spent').text(current);

                    var totalled = 0;
                    $("#eq .slider").each(function () {
                        totalled += parseInt($(this).parent('div:eq(0)').find('.spent').text());
                        spend(totalled);
                    });
                }
            });
        });
+1  A: 

Try:

.....

slide: function (event, ui) {
                    // set the current value to whatever is selected.
                    current = ui.value;
                    if(current > 300){
                       current = 300; //otherwise, it's stuck at 301
                       return false;
                     }

                   ....rest of your code
cinqoTimo
Hrnm. This technically does work, but it completely locks the sliders. Any idea on how I could still allow it to subtract from one?
Stacey
@Stacey - Try setting the current variable to 300 (or whatever your value is) just before you return false. That should work.
cinqoTimo
@Stacey - returning false will also trigger the "Stop" event, which you can use to unlock the slider, or reset it.
cinqoTimo
Thanks. I got most of what I want to work. I am having some additional problems, which I have outlined in a different post : http://stackoverflow.com/questions/3951726/jquery-slider-combined-values-having-difficulty-with-small-increments
Stacey