简体   繁体   中英

How to pass variable from one function to another Javascript?

I have two functions one which defines path based on if else conditions and I want to use that path in another function to plot a graph. catchit function is being called using onclick in HTML. This is what I have done but in console it is giving the value of path1 undefined. Any idea how I can make it work?

allpredicted.js

var path;         
 function catchit(no){    
    console.log(no)
    if (no ==1) {
        path="/static/data/submission1.csv"
      } else {
        path="/static/data/submission2.csv"
      
      } 
    parseData()
    }

 function parseData(createGraph) {
    var path1=path;
    console.log(path1);
    Papa.parse(path1,{
      download: true,
      complete: function(results) {
        createGraph(results.data);
        
      }
    });
  } 

cotton.html

<div  class="btn-group-vertical">
  
  
  <button class="block" id="1" button-id="1" onclick="catchit(1)" ><a  href={% url 'reports' %}>Formget Online Form Builder Create Online </a></button>
       <div class="space">
      </div>
     <button class="block"  id="2" value="2" button-id="2" onclick="catchit(2)" ><a  href={% url 'reports' %}>Formget Online Form Builder Create Online </a></button>
      <div class="space">
      </div>
     <button class="block"  button-id="3" onclick="catchit(3)" >Formget Online Form Builder Create Online Forms</button>
    
      
     <script src="{% static 'javascripts/allpredicted.js' %}"  ></script>
     
     
    
    
    </div>

Just add another argument to parseData .

var path;

function catchit(no) {
    console.log(no)
    if (no == 1) {
        path = "/static/data/submission1.csv"
    } else {
        path = "/static/data/submission2.csv"

    }
    parseData(createGraph, path);
}

function parseData(createGraph, path) {
    var path1 = path;
    console.log(path1);
    Papa.parse(path1, {
        download: true,
        complete: function(results) {
            createGraph(results.data);

        }
    });
}

catchit(no);

 var path function catchit(no) { path = [] if (no == 1) { path.push("/static/data/submission1.csv") } else { path.push("/static/data/submission2.csv") } parseData() } function parseData(createGraph) { var path1 = path.join(""); console.log(path1); } catchit(1) catchit(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM