Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: What's supposed to be the correct syntax to call function that has jQuery ajax within? Date: Wed, 14 Sep 2016 00:25:47 +0100 Organization: A noiseless patient Spider Lines: 47 Message-ID: <874m5jpekk.fsf@bsb.me.uk> References: <61ffa1ca-36ca-4081-a1f8-b7e51966bb50@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="20484"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5K/Bkq095osPZ2KpSz8mYCEZsZQ6FSaA=" Cancel-Lock: sha1:m5Pb+YIcgFcj4LaEEugOGKXqyg8= sha1:br2+oHLktyV/yy4kb2Pu6CJ7Ok0= X-BSB-Auth: 1.5fee517982d9d83a23d5.20160914002547BST.874m5jpekk.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:31364 justaguy writes: > I attempted to call a function by assign it to a variable's value but > it complained about "undefined", what is the correct way? Thanks. > > > This code exhibits a number of misunderstandings. It's probably best to take things one step at a time. The line Color = ckCounty('ga','TAYLOR'); assigns to Colour the value returned from the function call ckCounty('ga','TAYLOR'). But that call does not return a value explicitly so Color gets the value 'undefined'. What value do you think Color should have? 'green'? If so, you need to back up and learn a bit about how asynchronous requests work (that's the "A" in AJAX). By the time the server has handled the request and the success function gets called, the ckCounty function has long since finished and returned whatever value it was going to. -- Ben.