Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda