-1

In the following div tag, I just want to store the content "Bank Towers, Commissariat Road, M G Road Bangalore" into some variable. How could it be possible ?

<div class="InfoWindow1">
     <div class="InfoWindow">
          Bank Towers, Commissariat Road, M G Road Bangalore
     </div>
     .
     <br/> 
     <br/>
     <div class="InfoWindow">
          <b>Working Hours: </b>
          <br/>
          08:30 - 21:00
     </div>
</div>

Thanks in advance for your help !!

Malkus
  • 3,686
  • 2
  • 24
  • 39
Chirag
  • 89
  • 2
  • 12

5 Answers5

0
var variable = document.getElementById('yourDivId').innerHTML;
Gintas K
  • 1,438
  • 3
  • 18
  • 38
0

Since you are not mentioning jQuery I assume pure javascript:

function getClassContent(className) {
    var elems = document.getElementsByTagName('*'), i;
    for (i in elems) {
        if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ') > -1) {
            return elems[i].innerHTML;
        }
    }
}

divContent = getClassContent("InfoWindow")

This is a modification of this answer: How to Get Element By Class in JavaScript?

Community
  • 1
  • 1
Pylinux
  • 11,278
  • 4
  • 60
  • 67
0

You could place the contents into the div as data using jQuery. You will need to give the div an ID though.

<div id="example" class="InfoWindow">
<script>
    $("#example").data("some_data", "Bank Towers, Commissariat Road, M G Road Bangalore");
</script>

whenever you need the data, just call...

$("#example").data("some_data");

Here's a quick demo: http://jsfiddle.net/fQBJB/

ttay24
  • 21
  • 4
0

simply you can give an id for the divs and access the values using that id. For example,

<div class="InfoWindow1"><div class="InfoWindow" id="div1">Bank Towers, Commissariat Road,     
M G Road Bangalore</div>.<br/><br/><div class="InfoWindow"><b>Working Hours: </b> 
<br/>08:30 - 21:00</div></div>

<script>
 var x=document.getElementById('div1').innerHTML;
 alert(x);
</script>  
shemy
  • 573
  • 1
  • 5
  • 15
0

You can do this by simply iterating over the elements and using textContent property of nodeElement.

elems = document.getElementsByClassName('InfoWindow'); 
// can be any desired selector type.

for( var i = 0; i < elems.length; i++){
    console.log(elems[i].textContent);
}

textContent Docs

Working Example

Faisal Sayed
  • 783
  • 4
  • 12
  • Thanks for inf... I have designed Google Maps showing multiple markers. On clicking any marker, infowindow appears with corresponding marker's information. I want to store that inf in variable. Above code manage to store that but it doesn't shows right inf for each marker. what could be solution for that. Could you please refer following url where i have mentioned in details about requirement --- http://stackoverflow.com/questions/17940144/how-to-store-the-most-recently-opened-content-of-infowindow-into-a-variable-in-g – Chirag Jul 31 '13 at 09:17