I am new to JavaScript and have been working on some practice projects to improve my skills.
What is the best way to retrieve the value property of an element using its ID?
I have tried the following code, which seems to be working:
var billAmount = document.getElementById("billamt").value;
However, I would like to use the querySelector method instead of getElementById. What is the correct way to do this?
So far, the code I have tried has not worked:
var billAmount = document.querySelector("#billamt").value;
const billamt = document.querySelector('#billamt').value;
function calculate() {
console.log(billamt * 0.25);
}
<label>How much did you pay?
<input id="billamt" type="text" placeholder="Bill Amount">
</label>
<button onclick="calculate()">Calculate</button>
You can view the entire source code of the project by clicking on this link: https://codepen.io/anthony-bahinting/pen/dybegMW