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

128
Visualizações
Access to JS Object outside the function

I want print data from my JSON in HTML Code

<body>
    <div id="example"></div>
    <div id="testing"></div>
    <div id="demo"></div>
</body>
 
<script>
  
 loadDoc()
    
 function loadDoc() {
  const xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    let data= JSON.parse(this.responseText);
    let example = document.querySelector('#example');
    example.innerHTML = JSON.stringify(data.lose[0]["Preis"]);    <---- here it works, i can change the innerHTML of "example"
    
    }
  };
     
  xhttp.open("GET", "lose.json");
  xhttp.send();

}

testing.innerHTML = JSON.stringify(data.lose[1]["Preis"])    <---- outside the function loadDoc() i dont got access to data and i cant use the JSON data.

</script>
    
    
</html>

missingreference

So my Question how can i get access to my JSON data outside this loadDoc() function, because i want to use multiple entries of the imported JSON in my HTML.

about 4 years ago · Juan Pablo Isaza
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