Sometimes Code
Sometimes Code

Reputation: 601

JQuery DatePicker not working

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?

Upvotes: 0

Views: 134

Answers (2)

mkoryak
mkoryak

Reputation: 57928

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);
}

Upvotes: 4

mvb13
mvb13

Reputation: 1584

You can do it in such a manner:

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

Upvotes: 0

Related Questions