This javascript function is giving an error - function handleBetClick() { // Deduct 5…
- User
This javascript function is giving an error - function handleBetClick() { // Deduct 5 from the purse variable purse -= 5;
// Assuming you have a reference to the parent div element and the labels/buttons you want to remove var parentDiv = document.getElementById('results'); // Get reference to the parent div
// Get references to both labels and radio buttons var redLabel = document.querySelector('label[for="red"]'); // Get reference to the red label var blueLabel = document.querySelector('label[for="blue"]'); // Get reference to the blue label var redRadio = document.getElementById('red'); // Get reference to the red radio button var blueRadio = document.getElementById('blue'); // Get reference to the blue radio button
// Check which radio button is checked var chosenColor = ""; if (redRadio.checked) { chosenColor = "red"; } else if (blueRadio.checked) { chosenColor = "blue"; }
// Write the chosen color to the results element if (chosenColor) { parentDiv.innerHTML = "You bet on " + chosenColor + " $" + purse;; } else { parentDiv.innerHTML = "Please choose a color to bet on."; }
// Add the Start Race button var startRaceButton = document.createElement('button'); startRaceButton.textContent = "Start Race"; startRaceButton.setAttribute('onclick', 'handleButtonClick()'); // Set onclick event handler parentDiv.appendChild(startRaceButton);
}
- I think I might need to use event delegation on a parent element to handle radio button clicks
- Assistant
There are a couple of considerations and potential issues with the code you've provided:
-
The variable is being used, but it's not clear whether it's defined globally or in a larger scope that includes this function. If is not defined, it will throw a ReferenceError.
-