Javascript into a form field HELP

I need some help from the JS experts here, I need to make this javascript fill the latitude and longitude values into two form fields instead of printing it onto the div tags like it does as it is now.

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;	


The form fields have the id ‘lat’ and ‘lon’. I no clue on how to make the values being filled into the form when body load or by clicking the button. Hopefully someone can help me! :smile:

use document.getElementById('lat').value to reference that input field and just change the id to access the other input.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.