1

the td id='data' has the value below

['car', 7],['jeep', 2],['taxi', 1]

I want to get the value of 'td tag' and put on "Google Chart" arrayToDataTable

i tried this but not working

var x = document.getElementById('data').innerHTML; var data = google.visualization.arrayToDataTable([ ['Data', 'Value'], x
]);

Can anyone tell me how to do it.... need help

Bee
  • 309
  • 3
  • 14

1 Answers1

3

you need to add brackets and make an array out of the string (innerHTML / innerTEXT) that you get from the TD and then add those rows.

Be sure the TD is inside TR and that is inside TABLE, or use a DIV instead of a TD

var data = new google.visualization.DataTable();
data.addColumn('string', 'Cartype');
data.addColumn('number', 'Amount');

//var tdString = "['car', 7],['jeep', 2],['taxi', 1]";
//var tdString = document.getElementById('data').textContent;
var tdString = document.getElementById('data').innerHTML;

var arr = eval("[" + tdString + "]");

data.addRows(arr);

Anyway: working fiddle here : https://jsfiddle.net/L3tNE/15/

4nti
  • 196
  • 1
  • 14
  • @4nti- **how to do it in this format below** var tdString = document.getElementById('data').innerHTML; var arr = eval("[" + tdString + "]"); var data = google.visualization.arrayToDataTable([ ['Trips', 'Trip Value'], ---->**arr** ]); ----doesnt work – Bee Jun 10 '15 at 01:05
  • sir... can you please show me how to display the data from select tag "options value".... – Bee Jun 11 '15 at 02:44
  • Not exactly sure what you are asking but I assume you are searching for this : http://stackoverflow.com/questions/18113495/javascript-get-list-of-all-values-in-select-box. Please note that if these comments do not contribute to the original question it is better to ask a new question or ofcourse first search the website because I'm pretty sure you will find what you need if you do a little bit of searching here on stackoverflow. – 4nti Jun 11 '15 at 08:45