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

126
Views
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
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!