javascripthtmljqueryjquery-eventsjquery-ui-slider

JQuery Slider: Calling function without ID but with the object


I'm implementing a Slider with JQuery and I have the following problem.

The function is called upon load and it generates a slider with the Id from the div-element:

$(function() {
        $( "#slider" ).slider({
            value: 0,
            min: 0,
            max: 10,
            step: 1,
            slide: function( event, ui ) {
                $( "#value" ).val(ui.value );
            }
        });
        $( "#value" ).val( $( "#slider" ).slider( "value" ) );
    });

<div id="slider" >

I would rather have it called upon loading and pass the object -something like this:

function createSilder(object) {
        object.slider({
            value: 0,
            min: 0,
            max: 10,
            step: 1,
            slide: function( event, ui ) {
                $( "#value" ).val(ui.value );
            }
        });
        $( "#value" ).val( $( "#slider" ).slider( "value" ) );
}

<div id="slider" onload="createSlider(this)">

Is this possible?


Solution

  • Have you tried your code?

    There is probably no problem, it just depends on the way the plugin has been coded.

    Just be careful to pass the jQuery object : <div id="slider" onload="createSlider($(this));">.