Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geochart: trigger a function on 'select'

Here's the code for the geochart map I'm trying to implement on my site:

 <script type='text/javascript'>
 google.load('visualization', '1', {'packages': ['geochart']});
 google.setOnLoadCallback(drawRegionsMap);

  function drawRegionsMap() {
  var data = google.visualization.arrayToDataTable([
          ['Country'],
          ['Italy'],
          ['Germany'],
          ['France'],
          ['Turkey'],
          ['Indonesia']
  ]);

  var options = {
  };

  var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
  
    chart.draw(data, options);
  
  google.visualization.events.addListener(chart, 'select', function() {
    var selectedItem = chart.getSelection()[0];
    if (selectedItem) {
      var country = data.getValue(selectedItem.row, 0);
      if (country = 'France') { alert ('ciao') };
    }
    
  });
    
};

Would I would like to achieve is that if a user selects a particular region (for example France) a Javascript function is called. Now, the variable country works correctly (if you press France it sets to France) but there must be something wrong with if (country=) because it carries the same action even if I select a country that isn't France.

Any help would be much appreciated.

like image 806
beitz Avatar asked Feb 04 '13 18:02

beitz


1 Answers

You need to use equality operator == instead of assignment operator = here:

if (country == 'France') { alert ('ciao') };

Otherwise, you are setting country equal to 'France' and the if statement will always be true.

like image 190
Rob Grzyb Avatar answered Oct 04 '22 19:10

Rob Grzyb