-1

I am trying to get the variable value of data.latitude (line 483) out of the jquery.getAddress-4.0.0.js file into my index.html. I created a new line after line 483 and created a new variable in which I set data.latitude by doing so:

if (self.$output_fields.latitude) {
self.$output_fields.latitude.val(data.latitude || "");
var userLatitude = data.latitude;
}

Then in index.html I use the following code to retrieve the variable

$( document ).ready(function() {
    $('#postcode_lookup').getAddress({
        api_key: '', 
        output_fields:{
            line_1: '#line1',
            line_2: '#line2',
            post_town: '#town',
            postcode: '#postcode',
            latitude: '#latitude',
            longitude:'#longitude'
        },
        onAddressSelected: function(elem,index){
            console.log(userLatitude);
        }
    });

But it doesn't seem to work and I get the following error: getaddress.html:51 Uncaught ReferenceError: userLatitude is not defined

Here is the link to the .js file https://getaddress-cdn.azureedge.net/scripts/jquery.getAddress-4.0.0.js

Does anyone know how to get this variable into the index.html file?

  • You can't declare a variable in one function and reference it in another else except under very specific circumstances. – Heretic Monkey Oct 01 '20 at 18:51
  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Heretic Monkey Oct 01 '20 at 18:51

2 Answers2

0

According to the type of error, it doesn't seem to be the problem of the .js file- https://getaddress-cdn.azureedge.net/scripts/jquery.getAddress-4.0.0.js .

Also, var userLatitude = data.latitude; line is not mentioned in the .js file i.e. https://getaddress-cdn.azureedge.net/scripts/jquery.getAddress-4.0.0.js .

So most probably, compiler was unable, to find userLatitude varchar variable.

Make sure you are including the below line specifically at the end of the body content (i.e. just before the ending of the body tag) of the HTML code/file.

<script type="text/javascript" src="<your js file path>"></script>

Thanks!

mrd210
  • 21
  • 3
  • I made a copy of that file on my computer and added that line myself. It just seems that I cannot get the variable outside of that function – Daniil Vnoutchkov Oct 01 '20 at 19:10
0

Try...

if (self.$output_fields.latitude) {
   self.$output_fields.latitude.val(data.latitude || "");
   window.userLatitude = data.latitude;
}

Then you can get like window.userLatitude

Luã Melo
  • 113
  • 3
  • 10
  • when I do that then console.log outputs 'undefined' – Daniil Vnoutchkov Oct 01 '20 at 19:09
  • if you're put this variable in window instance, I don't know why you can't able to get this. Try to read on chrome console for window.userLatitude after load the code. Or put a breakpoint on this line, that create this window variable to know if the code it's passing from there. – Luã Melo Oct 01 '20 at 19:17