Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Passing data up through nested functions in .js

Coding beginner here. I've got a page-js script below which when a dropdown is changed it triggers function getDrawNumOne, which in turn runs a function and retrieves two pieces of data from a GoogleSheet (drawNumReturnOne[0] & [1]). I want to pass the returned drawNumReturnOne[1] up through getDrawOneNum and then on into doStuff for use there - but am struggling on how to do this in a slick way, given I cannot call variables outside of their function scope. Do I need multiple returns up through the function stack or is there another, better way? All advice gratefully accepted, thanks.

document.getElementById("btn").addEventListener("click",doStuff);
document.getElementById("m_p1").addEventListener("onchange",getDrawNumOne);
   
function doStuff(){    
   var userInfo ={};
   userInfo.m_p1 = document.getElementById("m_p1").value;
   userInfo.m_s1 = ######;
   google.script.run.userClicked(userInfo);
    
   }
   
function getDrawNumOne(){
var drawNumOne = document.getElementById("m_p1").value;
   google.script.run.withSuccessHandler(updateDrawNumOne).getDrawNo(drawNumOne); 
   
}
function updateDrawNumOne(drawNumReturnOne){
   document.getElementById("draw1").value = drawNumReturnOne[0];
   ###### = drawNumReturnOne[1];
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your situation, as a simple method, how about the following modification?

Modified script:

let sample = ""; // Added

document.getElementById("btn").addEventListener("click", doStuff);
document.getElementById("m_p1").addEventListener("change", getDrawNumOne); // Modified

function doStuff() {
  var userInfo = {};
  userInfo.m_p1 = document.getElementById("m_p1").value;
  userInfo.m_s1 = sample; // Modified
  google.script.run.userClicked(userInfo);
}

function getDrawNumOne() {
  var drawNumOne = document.getElementById("m_p1").value;
  google.script.run.withSuccessHandler(updateDrawNumOne).getDrawNo(drawNumOne);
}

function updateDrawNumOne(drawNumReturnOne) {
  document.getElementById("draw1").value = drawNumReturnOne[0];
  sample = drawNumReturnOne[1]; // Modified
}
  • As another modification point, I thought that in your script, onchange of document.getElementById("m_p1").addEventListener("onchange",getDrawNumOne); is change. But, if your situation can be used as onchange, please use it.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!