I'm trying to obtain the users location, then insert into two seperate <input> fields. The script works when trying to print to, <p id="lat"></p>. But not for the <input>.
<form>
<input type="number" step="any" name="lat" id="lat" value="" />
<input type="number" step="any" name="lng" id="lng" value="" />
</form>
<button onclick="getLocation()">Get current location</button>
<script>
var x = document.getElementById("lat").value;
var y = document.getElementById("lng").value;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = position.coords.latitude;
y.innerHTML = position.coords.longitude;
}
</script>
What am I missing here? Have added .value to the end of the veriable, am wondering if its the data type and/or step values?