I have a web page with multiple Google Charts.
Underneath each chart I'd like to add a button:
When a button is clicked, then a table featuring same data should be shown or hidden underneath it.
Displaying charts, buttons, tables works fine for me.
And they have ids: ..._chart
, ..._button
, ..._table
.
However getting the click handler to toggle a table doesn't work and the console.log
in the code below always prints the same id string.
I suspect it is a function closure issue, but I'm not sure how to workaround it.
Here is my faulty code:
function drawCharts() {
for (var csv in data) {
......skipped some code...
var chart = new google.visualization.LineChart(document.getElementById(csv + '_chart'));
chart.draw(t, options);
var table = new google.visualization.Table(document.getElementById(csv + '_table'));
table.draw(t, options);
$('#' + csv + '_table').hide();
$('#' + csv + '_button').click(function() {
console.log('#' + csv + '_table'); // ALWAYS PRINTS SAME
$('#' + csv + '_table').toggle();
});
}
}
$(function() {
google.setOnLoadCallback(drawCharts);
});
Any help please?