2

I don't know why jsGrid works in $(function() (sample) but it doesn't work in jQuery(document).ready (sample 2)

The error I get in sample 2 is : Uncaught TypeError: Cannot set property '_grid' of undefined

I need to make it work as it is on sample 2 jQuery(document).ready

Den
  • 283
  • 1
  • 9
  • Did you read [What is the difference between these jQuery ready functions?](https://stackoverflow.com/q/2662778/1287812) and the related ones? Please, add your code here instead of a JSfiddle. If the error is not solved with the above, I suggest a title like "jsGrid not working with jQuery(document).ready" – brasofilo May 28 '18 at 22:11
  • 1
    This is a hoisting problem. Can't put variable declarations in that function below where you try to access them `friends` and `countries`. The `var` will get hoisted but will be undefined at point you try to access it until the assignment is reached. By then you have already passed `undefined` to plugin – charlietfl May 28 '18 at 22:18
  • 1
    Simple fix might be helper functions for `getFriends()` and `getCountries()` so the data can reside below and keep the business logic at top – charlietfl May 28 '18 at 22:24
  • @charlietfl Thank you so much! works now https://jsfiddle.net/8hw0wcjj/2/ – Den May 28 '18 at 22:37

1 Answers1

1

This occurs because countries is undefined. In the working case, you define this variable in the global scope. But, in the second sample, you define it after you use this variable to create the grid.

Gosha_Fighten
  • 3,838
  • 1
  • 20
  • 31