So I have my JS Function that checks if a radio button is selected, then displays the value on my HTML like normal.
My problem is it only works if the default value is set to 0. How would I change this so that if I use the checked option on $5 in my HTML radio input, that it will also display that value in my browser?
If I change my checked option to a different tip value, it still shows 0 as the starting value since I didn't click anything.
Here is my JS Code
window.tip = 0;
const tipTotal = document.getElementById("tip");
const orderTotal = document.getElementById("total");
document.addEventListener("click", ({
target
}) => {
if (target.className === "tips" && target.checked) {
window.tip = parseInt(target.value);
} else {
return;
tipTotal.textContent = `$${window.tip}`;
}
I display the selected radio button here
<td id="tip">$0.00</td>
Here is my HTML Radio Buttons
<input type="radio" value="0" id="tip0" name="tip" class="tips" />
<label for="tip0">$0</label>
</div>
<div>
<input type="radio" value="300" id="tip1" name="tip" class="tips" />
<label for="tip1">$3</label>
</div>
<div>
<input type="radio" value="500" id="tip2" name="tip" class="tips" checked //here is the default checked option for $5 />
<label for="tip2">$5</label>
</div>
So by default, my app will show the tip: $5 option checked, however, the HTML on the browser for Tips will says Tip: $0.00 instead of Tip: $5.00 unless I click it
<td id="tip">$5.00</td>?