Question: Hello I am learning Google Map API from w3schools. I am new to JS and want to assign `location.lat()` and `location.lng()` to JavaScript variables. This is the copied code of tutorials. I am trying to assign the values as; 

`var latid=location.lat();` but I think `lat()` is returning nothing.

I have to save coordinates in database. If it is the wrong approach then please tell me what can I do to save `lnglat` coordinates in my database.

```
<!DOCTYPE html>
<html>
<head>
    <script src="http://maps.googleapis.com/maps/api/js?key=mykeyishidden&sensor=false"></script>
    <script>
        var map;
        var myCenter=new google.maps.LatLng(51.508742,-0.120850);

        function initialize() {
            var mapProp = {
                center:myCenter,
                zoom:5,
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };

            map = new google.maps.Map(document.getElementById("googleMap"),mapProp);

            google.maps.event.addListener(map, 'click', function(event) {
                placeMarker(event.latLng);
            });
        }

        function placeMarker(location) {
            var marker = new google.maps.Marker({
                position: location,
                map: map,
            });

            var infowindow = new google.maps.InfoWindow({
                content: 'Latitude: ' + location.lat() + '<br>Longitude: ' + location.lng()
            });

            infowindow.open(map,marker);
        }

        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>

<body>
    <div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>

```

Answer: 
getElementsByClassName returns a list of elements, not a single element. So you'll want to get the first element from it, which actually refers to your div. The code should look something like this:

```
var pElements = document.getElementsByClassName("normal");
var pElement = pElements[0];
    pElement.onclick = function () {
       if (this.getAttribute("class") == "normal")
         this.setAttribute("class", "change")
       else
         this.setAttribute("class", "normal");
    };

```

Demo: <http://jsfiddle.net/2QqU5/>

As RobG mentioned, `document.getElementsByClassName()` isn't supported by older browsers still in use. This is mainly IE8 and below. As an alternative, you can use `document.querySelectorAll(".normal")`. Notice the `.` in front of the classname (it is a CSS selector). Since you only need one element, you can also use `document.querySelector(".normal")`, to get just that one element. 
This might actually be easier, since these are the selectors that jQuery uses as well, so it might be easier to switch back and forth between native an jQuery.

And you can set the class using the `className` property, instead of using get/setAttribute.

Bringing all that together, the updated code looks like this:

```
var pElement = document.querySelector(".normal");
    pElement.onclick = function () {
       if (this.className == "normal")
         this.className = "change";
       else
         this.className = "normal";
    };

```

Updated demo: <http://jsfiddle.net/2QqU5/2/>
If browser support isn't an issue, you could use the [`toggle()` method/`classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element.classList#Summary).

[**Example Here**](http://jsfiddle.net/Qhsb2/)

```
(function () {
    var pElement = document.getElementsByClassName("normal")[0];
    pElement.addEventListener('click',function(){
       pElement.classList.toggle('change');
    });
} ());

```

[Browser support for `classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element.classList#Browser_compatibility).