Leaflet update position on dragend of marker
<p>I want to update the value of two hidden fields on my form based on marker position. I read this <a href="https://stackoverflow.com/questions/18575722/leaflet-js-set-marker-on-click-update-postion-on-drag">link</a> to get my answer, and this is my final code</p>

<pre><code>&lt;script&gt;
    var map = L.map('map', {
        center: [33.505, 53.9],
        zoom: 5
    });
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&amp;copy; &lt;a href="http://osm.org/copyright"&gt;OpenStreetMap&lt;/a&gt; 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);
&lt;/script&gt;
</code></pre>

<p>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?</p>

<p>And why would the following code does not work while <code>getElementById</code> does?</p>

<pre><code>$("#map_lat").val() = latValue;
</code></pre>

<p>Any help would be appreciated.</p>