Im trying to get the value of my input field with vanilla javascript to create a tip calculator.
For some reason, the value returns but as empty. I have tried innerText, innetHTML, value, and it doesn't work.
Can someone point me what is my mistake here?
const calculateTipBtn = document.getElementById('calculate');
const totalBill = document.getElementById('totalBill').value;
const resetBtn = document.getElementById('reset');
calculateTipBtn.addEventListener('click', () => {
if (totalBill.value === '') {
console.log('Value can\'t be empty')
} else {
alert('there is something')
console.log(totalBill)
}
console.log(totalBill)
})
resetBtn.addEventListener('click', () => {
window.location.reload()
})
<div id="wrapper">
<header>Vanilla Tip Calculator</header>
<div class="content">
<label for="totalBill">Enter Total bill</label>
<input type="text" name="total" id="totalBill">
<label for="tipPercentage"> How much tip are you feeling?</label>
<input type="checkbox" name="tip" value="15"><span>15%</span>
<input type="checkbox" name="tip" value="20"><span>20%</span>
<input type="checkbox" name="tip" value="30"><span>30%</span>
<br>
<button id="calculate">Calculate!</button>
<button id="reset">Reset</button>
</div>
<div class="tip-result"></div>
</div>
