0

I'm doing something wrong, and I can't find the error.

I'm stumped why I cannot change the innerHTML with this...

some text I want to change.
<p>Click the button to get your coordinates.</p>
<button onclick="showPosition ()">Use My Location</button>

<!-- TODO: move this to an external script -->
<script>
var targetDiv = document.getElementsByClassName('targetDiv');

function showPosition () {
    alert(targetDiv);    // alerts the element fine
    targetDiv.innerHTML = 'Foo';    // does not work
    document.getElementsByClassName('targetDiv').innerHTML = 'foo';  // does not work either.
}
</script>
Johnny5
  • 463
  • 2
  • 5
  • 16
  • 3
    getElementsByClassName() returns an array-like object - an HTMLCollection - of matching elements. You'll have to apply changes to each element of the array. – Surreal Dreams Nov 11 '14 at 21:05
  • I'd say you only have 1 targetDiv so make it `document.getElementsById('targetDiv');` and make the id of the div taegetDiv. Like Surreal dreams said, getElementsByClassName returns and array, hence the Elements and not Element – Billy Nov 11 '14 at 21:10

0 Answers0