user966582
user966582

Reputation: 3333

jQuery error, variable is not defined

I'm trying following code to create a plugin. I'm getting error at the line if(options.controls == true)

The error I get is 'options is not defined'.

How should I define it?

(function($) {

    $.fn.jwslider = function(options){
        var defaults = {            
            controls: true           
        };      
        var options = $.extend(defaults, options);      
    }

    init();

    function init()
    {
        if(options.controls == true)
        {
            alert("controls true");
        }       

    } 

}(jQuery)); 

Upvotes: 1

Views: 1159

Answers (2)

jasonslyvia
jasonslyvia

Reputation: 2535

(function($) {

    $.fn.jwslider = function(options){
        var defaults = {            
            controls: true           
        };      
        var options = $.extend(defaults, options);      

        init();

        function init()
        {
            if(options.controls == true)
            {
                alert("controls true");
            }       
        } 
    }
}(jQuery)); 

Then the options is accessible inside init function

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172638

You have to define the options variable outside the scope of function.

Currently it is defined in the scope of $.fn.jwslider hence it is giving the error.

Upvotes: 1

Related Questions