I am new to HTML, CSS, and JavaScript, so please bear with me.
I am trying to create a form which has an element that uses geolocation to get the current location of a user when he/she checks a checkbox, and it inputs the coordinates inside a textbox that I've set up. This works fine, but when I uncheck the checkbox, the coordinates disappear along with the textbox. How do I clear just the coordinates without making the textbox disappear as well?
Below is my code:
function getLocation(myCheck) {
var x = document.getElementById("place");
if (myCheck.checked) {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else {
x.innerHTML = "Geolocation disabled or unavailable.";
}
function showPosition(position) {
x.innerHTML = position.coords.latitude + ", " + position.coords.longitude;
}
}
else {
x.innerHTML = "";
}
}
<h4> Coordinates: <label id="place"><input type="text"></label><label>Use current location? <input id="myCheck" onclick="getLocation(this)" type="checkbox"></label>
</h4>
Aucun commentaire:
Enregistrer un commentaire