Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Update global variable after click event (JavaScript)

It sounds super simple, but I am looking to update my global variables ('latitude' and 'longitude') every time the map is clicked, as need to use these values elsewhere. Updating the variables from inside the function does not apply. Thus, the alerts show 'undefined'.

Any guidance would be much appreciated :-)

// set global vars
var latitude;
var longitude;
var popup = L.popup();

// create function to populate pop-up and global vars
function onMapClick(e) {  // show pop-up when map is clicked
  popup .setLatLng(e.latlng) .setContent("You clicked the map at " + e.latlng.toString()) .openOn(mymap); // populate pop-up with lat/long values
  latitude = e.latlng.lat // populate global var to use elsewhere
  longitude = e.latlng.lng // populate global var to use elsewhere
}

mymap.on('click', onMapClick);  // call function when cliked on map

alert(latitude) // output is 'undefined'
alert(longitude) // output is 'undefined'
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The variables get updated. The only thing is that your alert calls have already happened. They aren't going to get done again when the click happens unless you write code to do them again when the click happens. Just move the alert calls (or whatever you're really doing with that information) into the click handler:

// set global vars
var latitude;
var longitude;
var popup = L.popup();

// create function to populate pop-up and global vars
function onMapClick(e) {  // show pop-up when map is clicked
    popup .setLatLng(e.latlng).setContent("You clicked the map at " + e.latlng.toString()).openOn(mymap); // populate pop-up with lat/long values
    latitude = e.latlng.lat; // populate global var to use elsewhere
    longitude = e.latlng.lng; // populate global var to use elsewhere
    alert(latitude);  // <===== Moved
    alert(longitude); // <===== Moved
}

mymap.on('click', onMapClick);  // call function when cliked on map

This also means you may well not need the globals at all:

// set global vars
var popup = L.popup();

// create function to populate pop-up and global vars
function onMapClick(e) {  // show pop-up when map is clicked
    popup .setLatLng(e.latlng).setContent("You clicked the map at " + e.latlng.toString()).openOn(mymap); // populate pop-up with lat/long values
    alert(e.latlng.lat);
    alert(e.latlng.lng);
}

mymap.on('click', onMapClick);  // call function when cliked on map
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!