Below, I have a simple form that has 4 checkboxes acting as seats. What I am trying to do is when a visitor chooses, say, seat checkboxes with IDs A2
and A4
, I want those IDs and their total value to be shown instantly after clicking inside a paragraph with which have a name called id="demo"
. When a button [Reserve Now] has been clicked, the total value should be assigned to a variable called $TotalCost
.
How can I accomplish this? Here’s my code:
<!DOCTYPE html> <html> <body> <h2>Please choose a seat to book</h2> <form action="/action_page.php" method="post"> <input type="checkbox" name="vehicle" id="A1" value="$100">$100<br> <input type="checkbox" name="vehicle" id="A2" value="$65"> $65<br> <input type="checkbox" name="vehicle" id="A3" value="$55"> $55<br> <input type="checkbox" name="vehicle" id="A4" value="$50"> $50<br> <p id="demo"> Selected Seat(s) <br> <br> Total: USD <input type="submit" value="Reserve Now"> </form> </p> <script> document.getElementById("A1").addEventListener("click", displayCheck); function displayCheck() { document.getElementById("demo").innerHTML = ; } </script> </body> </html>
Advertisement
Answer
Here’s one approach to setting up event listeners on checkboxes. I used document.querySelectorAll("input[type='checkbox']");
to fetch all of the checkbox elements from the DOM and a loop to add a listener to each checkbox. A selections
object can keep track of which items have been checked. When a checkbox is clicked on, the item values are added to the object by key. When the checkbox is off, the item is deleted from the object. Whenever an action happens, the DOM is updated with all relevant information based on the contents of selections
.
This example is just a quick sketch to give you the idea. You’ll need another event listener for your submit button to handle sending the form data to your PHP script. I’ll leave that as an exercise.
Note that the HTML you’ve provided is invalid because nesting is broken. A HTML validator can be helpful for fixing these sort of problems.
var selections = {}; var checkboxElems = document.querySelectorAll("input[type='checkbox']"); var totalElem = document.getElementById("seats-total"); var seatsElem = document.getElementById("selected-seats"); for (var i = 0; i < checkboxElems.length; i++) { checkboxElems[i].addEventListener("click", displayCheck); } function displayCheck(e) { if (e.target.checked) { selections[e.target.id] = { name: e.target.name, value: e.target.value }; } else { delete selections[e.target.id]; } var result = []; var total = 0; for (var key in selections) { var listItem = "<li>" + selections[key].name + " " + selections[key].value + "</li>"; result.push(listItem); total += parseInt(selections[key].value.substring(1)); } totalElem.innerText = total; seatsElem.innerHTML = result.join(""); }
<!DOCTYPE html> <html lang="en"> <head> <title>...</title> </head> <body> <h2>Please choose a seat to book</h2> <form action="/action_page.php" method="post"> <input type="checkbox" name="vehicle" id="A1" value="$100">$100<br> <input type="checkbox" name="vehicle" id="A2" value="$65"> $65<br> <input type="checkbox" name="vehicle" id="A3" value="$55"> $55<br> <input type="checkbox" name="vehicle" id="A4" value="$50"> $50<br> <p>Selected Seat(s)</p> <!-- container for displaying selected seats --> <ul id="selected-seats"></ul> <div> Total: $<span id="seats-total">0</span> USD <input type="submit" value="Reserve Now"> </div> </form> </body> </html>
Often, you’ll want to generate the elements dynamically and add event listeners. Here’s a toy example:
for (let i = 0; i < 1000; i++) { const checkbox = document.createElement("input"); document.body.appendChild(checkbox); checkbox.type = "checkbox"; checkbox.style.margin = 0; checkbox.addEventListener("mouseover", e => { e.target.checked = !e.target.checked; }); checkbox.addEventListener("mouseout", e => setTimeout(() => { e.target.checked = !e.target.checked; }, 1000) ); }
See also event delegation which lets you add a single listener on many child elements.