0

I am using jQuery DatePicker in my website. I want the two dropdown on datepicker : one is for selecting month and one is for selecting year. Therefore i should write in the below format:

     $('.datepicker').datepicker({ 
     changeMonth: true,
    changeYear: true
});

I want to make this picker reusable therefore i make a function which takes the format parameters like given below:

    function RegisterDateTimePicker(options) {

var test = "{" + options + "}";

$('.date').datepicker(test);
}

The parameter "options" above is a variable which consist of string

    "changeMonth: true, changeYear: true"

Now datepicker is not showing. The error i get while debugging is:

    Uncaught TypeError: Cannot call method 'apply' of undefined 

kindly provide the way to get rid of this problem?

2 Answers 2

4

options should be an object, not a string. you are doing it wrong!

if options were an object, you could change the function to this (to make it work):

function RegisterDateTimePicker(options) {
  $('.date').datepicker(options);
}

but its not an object, its a string, which is a bad idea. you should change it to an object unless you have a really good reason to keep it as string - which you should explain to me before i can help you any further.

edit: instead of passing in a string you should pass in an object maybe like this:

var options = { 
    changeMonth: true,
    changeYear: true
};
//now call your function:
RegisterDateTimePicker(options);

or maybe you want to have your resuable function always make the datepicker with the same options? then do this:

function RegisterDateTimePicker(options) {
  var options = { 
    changeMonth: true,
    changeYear: true
  };
  $('.date').datepicker(options);
}
2
  • oh..which type of object? And how can i change this string to that type of object? Commented Jul 4, 2013 at 12:29
  • you cant easily change the string to an object without parsing it. why is it a string in the first place? you should just construct an object and pass that! ill include an example in the answer
    – mkoryak
    Commented Jul 4, 2013 at 12:33
0

You can do it in such a manner:

var options ={changeMonth: true, changeYear: true};
RegisterDateTimePicker(options);

Not the answer you're looking for? Browse other questions tagged or ask your own question.