0

I'm using google maps for my website.Below code is working with HTML id element, and I need to use it in class elements.

<script type="text/javascript">
 function initialize() {
    var coords = document.getElementById('coords').innerHTML.split(",");
    var lat = coords[0];
    var lng = coords[1];

    var myOptions = {
    center: new google.maps.LatLng(lat, lng),
    zoom: 17,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);

  var myMarkerLatlng = new google.maps.LatLng(lat,lng);
  var marker = new google.maps.Marker({
      position: myMarkerLatlng,
      map: map,
      title: 'Hello World!'
  });
}
</script>

In the above code I'm using the below code line to get element by ID:

var coords = document.getElementById('coords').innerHTML.split(",");

But, I want to use this code to work with class elements.

Any solution would be great.

Thanks in Advanced.

1 Answers1

1
function getElementsByClassName(node, classname) {
    var a = [];
    var re = new RegExp('(^| )'+classname+'( |$)');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

tabs = getElementsByClassName(document.body,'tab');  // no document

Check the answer here enter link description here

Community
  • 1
  • 1
Sudip Pal
  • 2,041
  • 1
  • 13
  • 16
  • You will be getting the array list of similar match classes and the respective data in the array index value. Take the value and split it. – Sudip Pal Jul 14 '14 at 09:36