almendar
almendar

Reputation: 1813

Variable in coffeescript and jQuery does not work

datePicker = $("#datepicker")
$ ->
  $("#datepicker").datepicker()

The above works great, the belove does not.

datePicker = $("#datepicker")
$ ->
  datePicker.datepicker()

I cannot find a reason why the second one does not invoke the calendar control?

Upvotes: 0

Views: 53

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074949

You're registering a function to run at DOM ready. In your first example, you're not looking for the #datepicker element until the DOM is ready; in your second example, you're looking for it right away, but then trying to use it later on DOM ready.

I expect it's failing because this code is above the #datepicker element in the HTML, so the element doesn't exist yet when the code runs.

If you move the code to the bottom of the page, just before the closing </body> tag (which is generally a good idea anyway), both should work (and you can stop using the DOM ready callback).

Upvotes: 4

Related Questions