Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda