I want to update the value of two hidden fields on my form based on marker position. I read this link to get my answer, and this is my final code
<script>
var map = L.map('map', {
center: [33.505, 53.9],
zoom: 5
});
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
map.on('dblclick', addMarker);
var Marker;
function addMarker(e) {
//remove previous markers
if (Marker) {
map.removeLayer(Marker);
UpdateLatLng(e);
}
// Add marker to map at click location; add popup window
Marker = new L.marker(e.latlng, {
draggable: true
}).addTo(map);
}
function UpdateLatLng(e) {
var latValue, lngValue;
latValue = e.latlng.lat;
lngValue = e.latlng.lng;
document.getElementById("map_lat").value = latValue;
document.getElementById("map_lng").value = lngValue;
//$("#map_lat").val() = latValue;
}
function UpdateLatLng2(e) {
var latValue, lngValue;
var position = marker.getLatLng();
latValue = position.lat;
lngValue = position.lng;
document.getElementById("map_lat").value = latValue;
document.getElementById("map_lng").value = lngValue;
//$("#map_lat").val() = latValue;
}
Marker.on('dragend', UpdateLatLng2);
</script>
The result is that my form fields are not updated on the first double click event, but they get updated on the next double clicks or after marker's dragend event instead. Why?
And why would the following code does not work while getElementById
does?
$("#map_lat").val() = latValue;
Any help would be appreciated.