Hi there,
I am trying to get the browser device location using Javascript, then I'll to insert latitude and longitude in two different Input Text fields. Everything is working great to get the actual location but I can't insert the values on TXTloclat and TXTloclon. Any recommendation? I can't see why is not working. I Already try many ways but nothing. Javascript is not my best languaje, I'm learning now.
This is my code:
Thank you for your help.
I am trying to get the browser device location using Javascript, then I'll to insert latitude and longitude in two different Input Text fields. Everything is working great to get the actual location but I can't insert the values on TXTloclat and TXTloclon. Any recommendation? I can't see why is not working. I Already try many ways but nothing. Javascript is not my best languaje, I'm learning now.
This is my code:
Code:
<html>
<body>
<h1>HTML Geolocation</h1>
<p>Click the button to get your coordinates.</p>
<p id="demo" align="center">
<form id="form1" name="form1" method="post">
<h1 align="center" ><?php echo date('h:i a') ?></h1>
<div align="center">
<?php echo date('m-d-Y');?>
<br>
<input type="text" name="TXTloclat" id="TXTloclat"><input type="text" name="TXTloclon" id="TXTloclon">
<br>
<input type="text" name="TXT_pin" id="TXT_pin" onfocus="getLocation()" align="center">
<br>
<button id="Submit" value="Submit">Clock</button>
</div>
</form>
</p>
<script>
const x = document.getElementById("demo");
const lat = document.getElementById("TXTloclat");
const lon = document.getElementById("TXTloclon");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
lat.innerHTML = " value='" + position.coords.latitude + "' ";
lon.innerHTML = " value='" + position.coords.longitude + "' ";
}
</script>
</body>
</html>
Thank you for your help.