title: How to assign value of variable inside XHR.onload fuction outside the fuction to javascript varaible? in django
body: <p>I need help to assign value of 'hmmm' variable inside xhr.onload fuction to the js variable 'questions' outside xhr function,the value of 'hmmm' is json object whose structure is similar to the 'questions' variable outside function.
  i have tried searching for it but haven't yet come across  good solution.</p>
  <p>is there any way to assign value of 'hmmm' variable to 'questions' variable</p>
  <p>Much appreciated .</p>
  <p>'''</p>
  <pre><code>   let quiz = document.forms[&quot;welcome_form&quot;][&quot;quiz&quot;].value;
     const xhr=new XMLHttpRequest()
      const method='GET'
      const url=&quot;/quiz/dock/{{q.slug}}&quot;
      console.log(url)
      const responseType='json'
      xhr.responseType=responseType
      xhr.open(method,url)
      xhr.onload=function(){    
          console.log(xhr.response)
          const Server_response=xhr.response
          let hmm=Server_response.quiz_list
          console.log(hmm)
          
      }
      xhr.send()
  
  
      let questions = [
        {
          id: 1,
          question: &quot;What is the full form of RAM ?&quot;,
          answer: &quot;Random Access Memory&quot;,
          options: [
            &quot;Random Access Memory&quot;,
            &quot;Randomely Access Memory&quot;,
            &quot;Run Aceapt Memory&quot;,
            &quot;None of these&quot;
          ]
        },
        {
          id: 2,
          question: &quot;What is the full form of CPU?&quot;,
          answer: &quot;Central Processing Unit&quot;,
          options: [
            &quot;Central Program Unit&quot;,
            &quot;Central Processing Unit&quot;,
            &quot;Central Preload Unit&quot;,
            &quot;None of these&quot;
          ]
        },
        {
          id: 3,
          question: &quot;What is the full form of E-mail&quot;,
          answer: &quot;Electronic Mail&quot;,
          options: [
            &quot;Electronic Mail&quot;,
            &quot;Electric Mail&quot;,
            &quot;Engine Mail&quot;,
            &quot;None of these&quot;
          ]
        }
      ];
  
      console.log(questions)
      let question_count = 0;
      let points = 0;
  
      window.onload = function() {
        show(question_count);
  
      };
  
      function next() {
  
        
        // if the question is last then redirect to final page
        if (question_count == questions.length - 1) {
          sessionStorage.setItem(&quot;time&quot;, time);
          clearInterval(mytime);
          window.location.href='/quiz/end/'+quiz
          
        }
        console.log(question_count);
  
        let user_answer = document.querySelector(&quot;li.option.active&quot;).innerHTML;
        // check if the answer is right or wrong
        if (user_answer == questions[question_count].answer) {
          points += 10;
          sessionStorage.setItem(&quot;points&quot;, points);
        }
        console.log(points);
  
        question_count++;
        show(question_count);
      }
  
      function show(count) {
        let question = document.getElementById(&quot;questions&quot;);
        let [first, second, third, fourth] = questions[count].options;
  
        question.innerHTML = `
        &lt;h2&gt;Q${count + 1}. ${questions[count].question}&lt;/h2&gt;
        &lt;ul class=&quot;option_group&quot;&gt;
        &lt;li class=&quot;option&quot;&gt;${first}&lt;/li&gt;
        &lt;li class=&quot;option&quot;&gt;${second}&lt;/li&gt;
        &lt;li class=&quot;option&quot;&gt;${third}&lt;/li&gt;
        &lt;li class=&quot;option&quot;&gt;${fourth}&lt;/li&gt;
      &lt;/ul&gt; 
        `;
        toggleActive();
      }
  
      function toggleActive() {
        let option = document.querySelectorAll(&quot;li.option&quot;);
        for (let i = 0; i &lt; option.length; i++) {
          option[i].onclick = function() {
            for (let i = 0; i &lt; option.length; i++) {
              if (option[i].classList.contains(&quot;active&quot;)) {
                option[i].classList.remove(&quot;active&quot;);
              }
            }
            option[i].classList.add(&quot;active&quot;);
          };
        }
      }
  
  
   &lt;/script&gt;'''
  </code></pre>