abpetkov / powerange

iOS 7 style range slider
http://abpetkov.github.io/powerange/
423 stars 84 forks source link

callback with parameters ??? #20

Open wokung opened 9 years ago

wokung commented 9 years ago

Hey,

is it somehow possible to add parameters to the callback function?

that would make the callback much more useful, since it is possible to pass an index or a classname for the callback.

Would be great if you could implement that. It is not possible at the moment.

Thanks!

with kind regards

jorgechato commented 9 years ago

:+1: this will be great for multiples powerange.

for the one like me that need multiples powerange, this is my hack until we have a callback with parameters.

$(document).on("ready", function(){
    var cust = document.querySelectorAll('.js-customized');
    var initCust;
    var j = 0;
    for (i = 0 ; i < cust.length ; i++){
        initCust = new Powerange(cust[i], {
            callback: displayValue,
            hideRange: true,
            klass: 'power-ranger',
            min: 0,
            max: 100,
            start: 0
        });
    }

    $('.range-handle').mousedown(function(){
        j = $('.range-handle').index(this);
    });
    function displayValue() {
        $('.display-box')[j].innerHTML = cust[j].value + "%";
    }
});
wokung commented 9 years ago

Hey,

thanks for the workaround!!!

I used jquery.each for this, but i'm not happy with the solution.

mguinness commented 9 years ago

Since ECMAScript 5 added a bind method to function objects, you can assign the element to this and then use that within the function:

var opt = {};
opt.callback = function () {
    document.getElementById(this.id + '-display').innerHTML = this.value;
}.bind(this);
var powerange = new Powerange(this, opt);

In addition, you can also associate the powerange object to the element for later use within both callback and other functions.

jQuery.data(this, 'powerange', powerange);

It would be great if Alexander could mention these workarounds in Readme.md as it would be helpful to others using multiple controls.