Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Hi im having some code like this:

var News = new Array();

jQuery(document).ready(function () {

 ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");  

 some code here...

 function setUpList(){
 }

 }
 });

 function initialize(){

 im retrieving my list items and store them in my global array News

 then im calling the function setUpList(); in jquery document ready..

 }

But its not working to call the function setUpList, its says its undefined, how can i work this out?

Becouse i need to use both jquery and get the list with csom

share|improve this question

2 Answers 2

You are trying to call the function setUpList, which is defined within the ready function callback, from the initialize function which is outside the ready callback scope, and that's why you are getting undefined.

Just move your setUpList function outside, at the same level of the initialize function. Something like:

ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");  

function initialize(){
   jQuery(document).ready(function () { //something here  });
}

function setUpList(){}

If you are worried about cluttering the global namespace, you can put all your functions inside a global object that you'll use as a namespace.

Eg:

var MyLib = {}; // global Object cointainer
MyLib.initialize = function(){};
MyLib.setUpList = function(){};

ExecuteOrDelayUntilScriptLoaded(MyLib.initialize, "sp.js"); 
share|improve this answer
    
okey, and it will load the list items and all that –  Jarule Mar 12 at 11:55
    
@Jarule is that a question? –  MdMazzotti Mar 12 at 11:58
    
function intitalize have some csom code to retrieve list items, and it wont work to retrieve list items if its in the jquery document ready –  Jarule Mar 12 at 12:03
    
You didn't show us the full code. Adjust my snippet to suite your needs –  MdMazzotti Mar 12 at 12:12
    
yes the code is to long, 10 pages –  Jarule Mar 12 at 12:28

shouldnt the function be outside of the function?

var News = new Array();

jQuery(document).ready(function () {

 ExecuteOrDelayUntilScriptLoaded(initialize, "sp.js");  

 some code here...

 now call my function!!!!
 setUpList();
 });

 function setUpList(){
 }

 }

 function initialize(){

 }

you cant call a function that is within the same function! as you have created a function setUpList within the main function it cant find it as functions reside outside of each other! in your case it could only see initialize function and it threw that exception!

share|improve this answer
    
That dont work, it wont load my list items... –  Jarule Mar 12 at 11:57
    
are you loading the items in the initialize method? have a look here: sharepoint.stackexchange.com/questions/8602/… –  ali Sharepoint Mar 12 at 13:06
    
If i load it before the jquery document ready , then i cant call the function inside jquery document ready from intitialize method –  Jarule Mar 12 at 13:21
    
and if i have the initialize code inside jquery document ready then its not loading the list.. –  Jarule Mar 12 at 13:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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