javascript - How to get variable from within the function? -


i'm trying access myvariable outside function. please see code below:

var myvariable; function drawbasic(){ ...   myvariable = document.getelementbyid('chart_div3');   var chart = new google.visualization.linechart(document.getelementbyid('chart_div'));   google.visualization.events.addlistener(chart, 'ready', function () {         chart_div.innerhtml = '<img src="' + chart.getimageuri() + '">';          myvariable.innerhtml = chart.getimageuri();          myvariable = myvariable.innerhtml;          //prints out desired value         console.log(myvariable);   });  //prints out desired value console.log(myvariable); ... } //prints out undefined     console.log(myvariable); 

i know myvariable defined outside function hoping set inside function , value used anywhere else, wrong. question how can value of chart.getimageuri() , store value outside function drawbasic?

i think because of issue syntax in "var = myvariable". wrote , works fine.

var myvariable; drawbasic(); function drawbasic(){    myvariable = "test string";    //prints out desired value    console.log(myvariable); } //prints out undefined console.log(myvariable); 

jsfiddle example


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -