Reputation: 45
How would I need to write this to have the elements created in two places?
Here is my code.
I have tried using document.getElementsByName
and document.getElementsByClassName
and neither of them work.
HTML
<p style="display:block;" class="coords"></p>
Javascript
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position, coords)
{
var places = document.getElementsByClassName(coords);
for (var i = 0; i < places.length; i++)
{
places[i].innerHTML = "<input type='text' name='lat' id='lat' value='" + position.coords.latitude + "'>" +
"<input type='text' name='lon' id='lon' value='" + position.coords.longitude + "'>";
}
}
Upvotes: 0
Views: 63
Reputation: 780851
After calling getElementsByClassName
you must use a loop to update each of them.
function showPosition(position) {
var places = document.getElementsByClassName("coords");
for (var i = 0; i < places.length; i++) {
places[i].innerHTML="<input type='text' name='lat' id='lat' value='" + position.coords.latitude + "'>" +
"<input type='text' name='lon' id='lon' value='" + position.coords.longitude + "'>";
}
}
Upvotes: 2
Reputation: 20189
You may want to use
document.getElementsByClassName
or document.getElementsByName
with no capital letter at the start
Upvotes: 0