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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -