I'm making a small webapp in Javascript. I got every thing working except for 1 probably simple thing. I have 2 global variables that have to stay global, because I have to use them later in other functions:
var staffmembername;
var data = {
  "personaldata": [{
      "name": staffmembername,
      "location": "Iowa"
    },
    {
      "name": "Cynthia",
      "location": "California",
    }
  ]
};
The 'data' array uses the 'stafmembername' variable. The 'stafmembername' variable change its value to a selected option value. As you can see thanks to the test with alert. How ever the variable within the array seems to stay undefined.
Rest of code:
document.getElementById("confirmchoice").onclick = function() {
  var e = document.getElementById("staffmember");
  pickedname = e.options[e.selectedIndex].text;
  document.getElementById("nr1").value = pickedname;
  staffmembername = document.getElementById("nr1").value;
  alert(staffmembername);
  document.getElementById("h3tl").innerHTML = "name:" + data.personaldata[0].name + "<br>"
  + "location: " + data.personaldata[0].location;
} 
If I put the variables within the function it al works, but then my other functions won't work. So is there a way to update the variable within the array while keeping both variables global?
 
     
    