I am trying to gather information from a user by prompts that are each in their own functions. Each of these own functions work by themselves as I have tested them. Next I am trying to display the information in a Div using the function financialInfoDisplay(), but I am getting an following error message
Uncaught TypeError: Cannot set property 'innerHTML' of null at financialInfoDisplay
and the prompts are not showing show in the browser. Why is this and how can I fix it?
I have even tried include the code to add the .innerHTML to the div in each function, which works, but as soon as I add another prompt to the div the first one disappears.
I have even tried to add in parameters instead of the global variables such as var userWithdrawal, userName, depositAmount into the financialInfoDisplay() and then passing those variables as arguments when calling said function but that did not work either.
//GLOBAL VARIABLES
var userWithdrawal, userName, depositAmount;
//GETS ELEMENT FROM DOM
var $ = function (id) {
"use strict";
return window.document.getElementById(id);
};
//GETS USER NAME
function namePrompt () {
"use strict";
userName = window.prompt("Please enter your name");
return userName;
}
//GETS DEPOSIT AMOUNT
function depositAmountPrompt () {
"use strict";
depositAmount = window.prompt("Please enter the amount of money you would like to deposit");
return depositAmount;
}
//GETS WITHDRAWAL Amount
function withdrawalAmount () {
"use strict";
userWithdrawal = window.prompt("Please enter the amount of money you would like to withdrawal from your account");
return userWithdrawal;
}
//DISPLAYS THE PROMPTS WITHIN A DIV
function financialInfoDisplay() {
"use strict";
var infoDisplay = window.document.getElementById('infoDisplay').innerHTML = userName + depositAmount + userWithdrawal;
return infoDisplay;
}
financialInfoDisplay();
//HANDLES ALL EVENT LISTENERS
function init() {
"use strict";
$('nameBtn').addEventListener("click", namePrompt);
$('depositBtn').addEventListener("click", depositAmountPrompt);
$('withdrawlBtn').addEventListener("click", withdrawalAmount)
}
window.addEventListener("load", init);
<style>body {
width: 500px;
margin: 0 auto;
}
#infoDisplay {
border: 3px solid black;
height: 250px;
}
</style>
<body>
<button type="button" id="nameBtn" value>Name</button>
<button type="button" id="depositBtn">Deposit</button>
<button type="button" id="withdrawlBtn">Withdrawl</button>
<div id="infoDisplay"></div>
</body>