0

I use my getHTML() function in a different function which parses through data, and I display the data as an HTML string. When the user is done filling the form, I want to send that information along with the username and examid. The username and examid are also coming from the backend at the position data[0].username and data[1].examid How can I make these two attributes into global variables, so that I can use them in my send function?

  function getHTML(data){
     var htmlString = "";
        for(var i=0; i < data.length; i++){
            htmlString += "<p>"
                          + data[i].questionid + "." + "\n"
                          + "Question: " + data[i].question
                          + "\n" + "<input type='text' value1='" 
                          +data[i].username+ " value2='" +data[i].examid+ "'>";                 
           htmlString += '</p>';
        }
        response.insertAdjacentHTML('beforeend', htmlString);
    }   

   function send(){ 
           var inputText = document.querySelectorAll("input[type='text']");

            var data = []; 
            for(var index = 0; index < inputText.length; index++){

                input = inputText[index].value;
                data.push({'text' : input});
            }  
            data.push({'username' : username, 'examid' : examid});
      }

3 Answers 3

1

Define your variable out of any function so they would be global

var username;
var examid;

function(){...}
function(){...}
Sign up to request clarification or add additional context in comments.

Comments

0

window.username = data[i].username; and window.examid = data[i].examid.

Though you may be trying to store more than one in which case you want an array.

You are looking to use the window element if you want it in the global scope.

Comments

0

Try following code might help you

(function() {
    var x = document.getElementById("Node").value;
    document.getElementById("Node").onchange = function() {
      myNode()
    };
    document.getElementById("mySelect1").onchange = function() {
      myNotes()
    };
    document.getElementById("mySelect2").onclick = function() {
      summary()
    };

    function myNode() {
      x = document.getElementById("Node").value;
    }

    function summary() {
      var a = document.getElementById("mySelect1").value;
      var b = document.getElementById("mySelect2").value;
      document.getElementById("5").value = a + " - " + b + " - " + x;
    }

    function myNotes() {

      var a = document.getElementById("mySelect1").value;
      var b = document.getElementById("mySelect2").value;
      document.getElementById("4").value = a + " + " + b + " + " + x;
    }


  }





)();
Notes : <input type="text" id="4" /><br> Summary : <input type="text" id="5" /><br>
<select id="Node">
      <option value="w">w
      <option value="x">x
      <option value="y">y
      <option value="z">z
    </select>
<select id="mySelect2">
      <option value="a">a
      <option value="b">b
      <option value="c">c
      <option value="d">d
    </select>
<select id="mySelect1">
      <option value="1">1
      <option value="2">2
      <option value="3">3
      <option value="4">4
    </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.