Search code examples
javascriptjqueryhtmlrangeslider

RangeSlider.js is not working when i call from another js page


$element.rangeslider({

    // Deactivate the feature detection
    polyfill: false,

    // Callback function
    onInit: function() {
        valueOutput(this.$element[0]);
    },

    // Callback function
    onSlide: function(position, value) {
        console.log('onSlide');
        console.log('position: ' + position, 'value: ' + value);
    },

    // Callback function
    onSlideEnd: function(position, value) {
        console.log('onSlideEnd');
        console.log('position: ' + position, 'value: ' + value);
    }
});

i have called external rangeslider.js page also but still it saying $element.rangeslider is not defined.


Solution

  • Try this solution, where you will be waiting for the script to be loaded then calling your desired functionality.

    $.getScript( "YourJS.js" )
      .done(function( script, textStatus ) {
             console.log( textStatus );
              //Preform here
            }) ;
    

    The working example.

    init(1,10);
    function init(min,max){
       $("#io").append($("<div style='text-align:center;'><div><input type='range' id='slider' min='" + min + "' max='" + max + "' step='1' value='3' data-rangeslider><output id='val'></output></div></div>"));
        $('head').append('<link href="https://cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.2/rangeslider.css" rel="stylesheet" type="text/css">');
       $.getScript( "https://cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.2/rangeslider.js" )
           .done(function( script, textStatus ) {
             console.log( textStatus );
              //Preform here
              initRangeSlider($("#slider"));
            }) ;
    }
    
    function initRangeSlider(ele){
       ele.rangeslider({
    
        // Deactivate the feature detection
        polyfill: false,
    
        // Callback function
        onInit: function() {
            //valueOutput(this.$element[0]);
        },
    
        // Callback function
        onSlide: function(position, value) {
            console.log('onSlide');
            console.log('position: ' + position, 'value: ' + value);
        },
    
        // Callback function
        onSlideEnd: function(position, value) {
            console.log('onSlideEnd');
            console.log('position: ' + position, 'value: ' + value);
        }
       });
    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id='io'></div>