I stumbled upon this JavaScript code online:
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
link.click();
After running it, the file my_data.csv is generated and downloaded automatically. Now, I want to modify it so that when a user clicks a button, the code executes. I attempted to convert it into a function called Testing()
and updated my link as follows:
<a href="#" onClick="javascript:Testing()">Test</a>
However, placing the code inside a function seems to break it. What am I doing incorrectly?
The console error message states 'Testing is not defined.'
For reference, here's a jsfiddle: http://jsfiddle.net/F6CWF/