0

I have a Jquery function defined as :

jQuery(document).ready(function($){
function initAutoComplete(textBox, query, isMustMatch, isAjaxAfterKeyPress)
        {
           // autocomplete logic
        }

});

I am calling this function from outside document.ready as :

initAutoComplete($("#txt" + FromTo + "Country"), "WebAddr?srvList=Country&areaCd=OT&val=", true, false);

the initAutoComplete is not recognized which is correct as its inside the scope of document.ready().

I tried below code by hooking the function with window object :

window.initAutoComplete = function(textBox, query, isMustMatch, isAjaxAfterKeyPress)
        {  
            //autocomplete logic
        }

Now I called this function from outside document.ready as :

initAutoComplete($("#txt" + FromTo + "Country"), "WebAddr?srvList=Country&areaCd=OT&val=", true, false);

But I am unable to fix the reference error : initAutoComplete is not recognized. Any help is appreciated. Thanks!

theConstructor
  • 109
  • 2
  • 13
  • 3
    Why not declaring `initAutoComplete` outside of ready closure? – dfsq Sep 29 '14 at 22:01
  • If using `window.initAutoComplete = ..` did not fix the error, then that line was not run *prior* to trying to calling `initAutoComplete`. The error is perfectly explainable, and is correct - so what tries to call what? Where *and* when are important. – user2864740 Sep 29 '14 at 22:02
  • when I am using initAutoComplete() outside document.ready(), the autocomplete() function in my autocomplete.js is not recognized. – theConstructor Sep 29 '14 at 22:06
  • Just define `function initAutoComplete() {...}` outside of the `.ready()` handler at the top level. If you want it to be a global function so you can call it from anywhere, then just declare it as so and you can call it from anywhere without regarding to timing issues. – jfriend00 Sep 29 '14 at 22:44

2 Answers2

0

Edit: Changed reference to window to use the jQuery version as the vanilla version is not working.

When you define the function on the window like:

$(window).initAutoComplete = function(textBox, query, isMustMatch, isAjaxAfterKeyPress)
        {  
            //autocomplete logic
        }

Then you also need to call it on the window like so:

 $(window).initAutoComplete($("#txt" + FromTo + "Country"), "WebAddr?srvList=Country&areaCd=OT&val=", true, false);

The only other thing I would mention is to make sure that you calling it outside of the document.ready is in fact done after document.ready has fired.

  • Got the error : TypeError: window.initAutoComplete is not a function. I made sure that I am calling window.initAutoComplete when document.ready is fired. – theConstructor Sep 29 '14 at 22:34
  • Hmm Try using the jquery window object, that is how I've done this in the past, I will update the answer with this. –  Sep 29 '14 at 22:36
  • Sorry, but still the same error : TypeError: $(...).initAutoComplete is not a function. – theConstructor Sep 29 '14 at 22:42
  • Then It looks like the code calling it is in fact running before the code that defines it, perhaps look at Sesha's answer to see if that helps. –  Sep 29 '14 at 22:44
0

Check this discussion

window.onload vs $(document).ready()

The order of events depends on the browser you are using. There is a subtle difference. Read through the article and make necessary changes if possible to "onload" rather than "ready".

Community
  • 1
  • 1
Sesha Kiran
  • 199
  • 1
  • 4