Search code examples
javascriptjquerygoogle-mapsmaxlength

Maxlength in lat and long google maps


I'm trying to use the maxlength in input button to show the latitude and longitude of the google maps, but maxlenght doesn't work... What am I doing wrong?

var marker = new google.maps.Marker({
   draggable: true,
   position: {lat: -47.00, lng: 18.00},
   map: map,
   });
   google.maps.event.addListener(marker, 'dragend', function (event) {
   document.getElementById("lat").value = event.latLng.lat();
   document.getElementById("long").value = event.latLng.lng();
});

HTML:

Latitu: <input id="lat" name="lat" maxlength="6" value=""> <br />
Longi:  <input id="long" name="long" maxlength="6" value=""/>

Solution

  • From the specification:

    declares a limit on the number of characters a user can input

    When you set the value via JS it's not a input by the user.

    You'll need to limit the number of characters via JS:

    document.getElementById("lat").value = String(event.latLng.lat()).substr(0,6);