Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Build an Interactive Website >Google Maps Integration >Adding a Dynamic Map Challenge Exercise 6

The Challenge: Set the "mapElement" using the native "document" object and the method "getElementById" and pass in the id for the "map".

Error Message: Bummer! You need to call 'getDocumentById' on 'document' and pass in 'map'. Set the vairable 'mapElement' with the result of this method call.

My Code: var mapElement = new google.maps.Map(document.getElementById("map"), mapOptions);

I'm totally stumped. I've even copied and pasted the exact code from https://developers.google.com/maps/documentation/javascript/tutorial#HelloWorld with changes to the variable "mapElement" and id "map". I didn't think that getDocumentById was a valid JS Method? Any help would be greatly appreciated!

13 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Aimee,

It's just asking for the html element document.getElementById("map") not the Google maps map object (you'll be doing that in the next step).

Regards Andrew

This doesn't work in my code challenge. This is my code:

var mapElement = document.getElementById("#map");

This doesn't pass.

Needed to take out the ID selector, got it.

Thank you!

Hello!

I have also been having trouble with this challenge:

my code:

var mapElement = document.getElementById("map");

results in: Bummer! You need to call 'getDocumentById' on 'document' and pass in 'map'. Set the variable 'mapElement' with the result of this method call.

What on earth is getDocumentById and why does my code not work? I have restarted this challenge twice to make sure an error from a previous task has not caused this.

Many Thanks - Badge Master Zurg

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi there,

I've fixed the typo in the error getDocumentById => getElementById.

Can you paste in the surrounding code? That should pass.

Regards

Andrew

Thank you for your kind attention

I really appreciate that you have fixed the typo in this challenge as I'm sure this will help other users, however I am still unable to pass the challenge with the following code:

<!D0CTYPE html>
<html>
<head>
    <title>Map</title>
        <style type="text/css">
    body, html {
        margin:0;
        padding:0;
    }
    body, html, #map {
        width:100%;
        height:100%;
    }
    </style>
</head>
<body>
    <script type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAn8fk75K8kCdW6pQ4B7yoBHJNrUbleieY&sensor=false"></script>

<ESCAPED! script type="text/javascript">
    var mapOptions = {
    center: new google.maps.LatLng(28.42,-81.58),
    zoom: 18,
    mapTypeId: google.maps.MapTypeId.SATELLITE,
  };

    var mapElement = document.getElementById('map');
    var theMap;
</script>

  <div id="map"></div>
</body>
</html> 
Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

The Issue is that the map div is after the script tag. It needs to be before the script tag. Because the element hasn't been parsed in to the dom yet and getElementById won't pick it up yet.

Place your div above the scripts and you'll be fine.

Thank you very much for your help this was indeed the problem and it now works!

I feel daft!

Badge Master Zurg (116 Badges Earned)

Wow, thanks @Andrew

A lot of time spent to solve this bug This should be corrected for the future!!!

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

Is there a bug still in? Can you tell me where?

hi Andrew: When i coded var mapElement = document.getElementById("map"); It didn't work,... I passed the challenge following your instructions given in the previous replies! Thank you!

I have been supplementing my treehouse learning with CodeAcademy and i wanted to ask why you don't need to put the DOM element "document" into brackets like this $(document)?

I almost had this code challenge the first time but i put the document into the format above thinking it was correct.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

Do they do this?

$(document).ready(function(){ 
//code here
})

If they do that means that they are including their JavaScript at the beginning of the page. If you include it at the end of the body it doesn't require it.

they do format their challenges like this, starting with the document, then followed by scripting.

Thanks for clearing it up for me!