1

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)); 
1
  • options is defined only inside the $.fn.jwslider scope, you have to declare it outside and then assign it inside that function(you will need to give a new name to the parameter of the function, maybe opts) Commented Sep 7, 2013 at 19:12

2 Answers 2

1
(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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.