I am trying to update and display a list of names each time the user submits a new name. For example, (first time: "Jim” is displayed, second time: “Jim Bob” is displayed, third time: “Jim Bob Joe” is displayed, etc.) It is required to use closure. I can get the pre-loaded input value, but I guess there is something wrong with my loop and I don't get any further input. Any ideas?
Enter a name and I will add it to the list:
<input type="text" id="newName" value="Doug"/>
<button onclick="closureExample()">closureExample()</button>
<p id="nameList">Abe,Bill,Charlie</p>
<script>
var addName = (function () {
var names = ['Abe', 'Bill', 'Charlie'];
var name = document.getElementById("newName").value;
var text;
for (var i = 0; i < names.length; i++) {
text += names[i];
}
return function () {return text;}
})();
function closureExample(){
document.getElementById("nameList").innerHTML = addName();
}
</script>