DjMist1080p
DjMist1080p

Reputation: 3

Save lat and lng on leaflet map click event

I am new to leaflet and js and I am looking for a way to get the latitude and longitude from a leaflet map on a click event and pass it into an HTML text input.

I tried to use

map.on('click', function(e) {
 var latitude = e.latlng.lat; 
 var longitude = e.latlng.lng;     
 var latt = document.getElementById("idfromHTML").innerHTML(latitude);
});

to save them on a variable, yet I cant seem to pass them in an HTML tag. Any help would be appreciated.

Upvotes: 0

Views: 872

Answers (1)

Falke Design
Falke Design

Reputation: 11338

If you have an <input> you need to add a value:

document.getElementById("idfromHTML").value = latitude;

if you have a other html element you need to change the innerHTML:

document.getElementById("idfromHTML").innerHTML = latitude;

Upvotes: 1

Related Questions