Search code examples
javascriptjqueryjquery-slider

JQuery slider with attribute for what to control, reusable prototype


In my project i intend to have multiple similar sliders therefore it would be interesting to make them using a prototype:

To set what the sliders control I want to use a "motorLabel". I have tried adding an attribute to the slider below but it does not find the attribute. Can you please help me change the code to a usable slider prototype.

$(function() {
    $('#svgbasics').svg({onLoad: drawOpenSwitch});
    //Slider for Last:
    $( "#slider" ).slider({
        motorLabel: 'm1',
        orientation: "horizontal",
        range: "min",
        min: 1,
        max: 200,
        value: 50,
        change: function( event, ui ) {
            $( "#amount" ).val( ui.value );

            console.log(this.motorLabel); // RETURNS UNDEFINED
            eval(this.motorLabel.motorSetLast(ui.value));

        }
    }).draggable();
    $( "#amount" ).val( $( "#slider" ).slider( "value" ) );

Solution

  • You can pass argument on html instead :

    <div id="slider" data-motor-label="m1"></div>
    

    And in your change function :

    $(this).data('motor-label');
    

    To get the value.