Search code examples
javascriptjqueryhtmlinputrangeslider

Rangeslider.js | Uncaught TypeError: Cannot read property '0' of undefined


I'm trying to use rangeslider.js

This is my HTML includes:

<script type="text/javascript" src="jquery/jquery-3.0.0.min.js"></script>
<script type="text/javascript" src="rangeslider.js-2.1.1/rangeslider.min.js"></script>

This is my HTML:

<input type="range" min="0.0" max="1.0" step="0.05" value="0.8" data-rangeslider="" style="position: absolute; width: 1px; height: 1px; overflow: hidden; opacity: 0;">

<script>
    // Initialize a new plugin instance for all
    // e.g. $('input[type="range"]') elements.
    $('input[type="range"]').rangeslider();

    // Destroy all plugin instances created from the
    // e.g. $('input[type="range"]') elements.
    $('input[type="range"]').rangeslider('destroy');

    // Update all rangeslider instances for all
    // e.g. $('input[type="range"]') elements.
    // Usefull if you changed some attributes e.g. `min` or `max` etc.
    $('input[type="range"]').rangeslider('update', true);
</script>

I am getting this error:

Uncaught TypeError: Cannot read property '0' of undefined.

These are the lines that appear to be being referencing to in rangeslider.min.js:

line 169: this.handleDimension = g(this.$handle[0], "offset" + i(this.DIMENSION)),

line 272: "string" == typeof b && e[b].apply(e, c)

I am unsure as to what type error I am causing?


Solution

  • You need to include the css file of rangeslider.js, and you shouldn't destroy the rangeslider.

    For a minimun example:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.2/rangeslider.css">
    </head>
    <body>
        <input type="range" min="10" max="1000" step="10" value="300">
        <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/rangeslider.js/2.3.2/rangeslider.js"></script>
        <script>
           $('input[type="range"]').rangeslider({polyfill: false});
        </script>
    </body>
    </html>

    The example above is based on the code in rangeslider.js.

    I think the Usage section in rangeslider.js is just a script showcase. So if we directly use those scripts, the following errors occurs:

    Error message in chrome:

    Uncaught TypeError: Cannot read property '0' of undefined

    Error message in Firefox:

    TypeError: this.$handle is undefined