简体   繁体   中英

getting the variable's value from another funtion

Is there a way to get the 'username' and 'questionid' variable's value in my send() function like how I did with inputText?

      var username;
      var questionid;

      function renderHTML(data) {
        var htmlString = "";
        var username = data[0].username;
        //var examid = data[1].examid;
        var questionid = data[2].questionid;
        for (var i = 0; i < data.length; i++) {
          htmlString += "<p>" + data[i].questionid + "." + "\n" + "Question: " + data[i].question + "\n" + "<input type='text'>";
          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
          });
        }
        console.log(data);

You do not need var before username and questionid in your renderHTML function, as they have already been defined:

  var username;
  var questionid;

  function renderHTML(data) {
    var htmlString = "";
    username = data[0].username;
    //var examid = data[1].examid;
    questionid = data[2].questionid;
    for (var i = 0; i < data.length; i++) {
      htmlString += "<p>" + data[i].questionid + "." + "\n" + "Question: " + data[i].question + "\n" + "<input type='text'>";
      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
      });
    }
    console.log(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM