I have a function to retrieve my current location based on longitude and latitude. However, I would like to auto populate a text area ( Populate Here) with my retrieved results, instead of prompting an alert.
<!DOCTYPE html>
<html>
<head>
<script>
function getPosition() {
var options = {
enableHighAccuracy: true,
maximumAge: 3600000
}
var watchID = navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
function onSuccess(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n');
};
function onError(error) {
alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
}
}
</script>
</head>
<body>
<div data-role="page" data-theme="b">
<div data-role="header">
<h1>Test</h1>
</div>
<div data-role="main" class="ui-content">
<form name="bmiform" id="bmiform" action="result.html">
<input type="hidden" name="actualbmi">
<label for="Location">Location:</label><button onclick = "onSuccess" id = "getPosition">PAN</button>
<input type="text" name="Location" id="location" placeholder = "Populate Here" (document.getElementsByName('onSuccess')[0].value)>
<br>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
So lets say you have a span in html
<span id="myspan"><span>
In your onSuccess function, you will set the innerHTML of that span, which is is between the opening and closing tags: <span>
innerHTML is here </span>
var span = document.getElementById("myspan");
var text = document.createTextNode(position.coords.latitude);
span.innerHTML = ''; // clear existing
span.appendChild(text);
You can try adding the longitude yourself. Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.