0

How could I make it so if a user clicks the radio button with the data-price of 5.99 then the price will show up in the total box but if the user clicks the "no charge" radio button then nothing will show up

var radio = document.querySelector("input[name=deliveryType]");
for (var i = 0; i < radio.length; i++) {

  if (radio.checked) {
    l_totalPrice += parseFloat(radio.dataset.price);
  }
}
total.value = l_totalPrice;
<section id="collection">
  <h2>Collection method</h2>
  <p>Please select whether you want your chosen event ticket(s) to be delivered to your home address (a charge applies for this) or whether you want to collect them yourself.</p>
  <p>
    Home address - &pound;5.99 <input type="radio" name="deliveryType" value="home" data-price="5.99" checked>&nbsp; | &nbsp; Collect from ticket office - no charge <input type="radio" name="deliveryType" value="ticketOffice" data-price="0">
  </p>
</section>

<section id="checkCost">
  <h2>Total cost</h2>
  Total <input type="text" name="total" size="10" readonly>
</section>
j08691
  • 204,283
  • 31
  • 260
  • 272

2 Answers2

0

Using event delegation and the dataset API, this is easy:

let total = document.querySelector("input[name='total']");

// Set the event listener up on an ancestor of both radio buttons
document.getElementById("collection").addEventListener("click", function(event){
  // When the event is triggered here, check the event.target to see what element
  // actually triggered the event. 
  if(event.target.value === "home"){
    // Use the dataset API to access the data-* attribute.
    total.value = event.target.dataset.price;
  } else {
    total.value = "";
  }
  
});
#collection h1, #checkCost h1 { font-size:1.9em; }
<section id="collection">
  <h1>Collection method</h1>
  <p>Please select whether you want your chosen event ticket(s) to be delivered to your home address (a charge applies for this) or whether you want to collect them yourself.</p>
  <p>
    Home address - &pound;5.99 
    <input type="radio" name="deliveryType" value="home" data-price="5.99" checked>&nbsp; | &nbsp; 
    Collect from ticket office - no charge 
    <input type="radio" name="deliveryType" value="ticketOffice" data-price="0">
  </p>
</section>

<section id="checkCost">
  <h1>Total cost</h1>
  Total <input type="text" name="total" size="10" readonly>
</section>

FYI: The first heading element within a section should always be an h1. If you don't want that font-size, just modify it with CSS. Never use an HTML element because of the default styling that a browser applies to it.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Here you go

function updateTotal(ele) {
 document.getElementById("total").value = ele.value;
}
<input type="radio" id="home" value="5.99" name="delivery_type" onchange="updateTotal(this)"/> 
<label for="home">Home address - <b>&pound;5.99</b></label>
<br>
<input type="radio" id="office" value="0" name="delivery_type" onchange="updateTotal(this)"/> 
<label for="office">Collect from ticket office - <b>no charge</b></label>
<br>
<br>
<input type="number" id="total" name="total" placeholder="Choose a delivery address" readonly/>

Let me know if you have any confusions :)

Utsav Barnwal
  • 985
  • 11
  • 14
  • 1
    [Inline HTML event attributes should not be used.](https://stackoverflow.com/questions/43459890/javascript-function-doesnt-work-when-link-is-clicked/43459991#43459991) It's 2020, not 1996. – Scott Marcus Dec 30 '19 at 20:26
  • I don't really prefer them but for the sake of simplicity for a new developer, I used it here. – Utsav Barnwal Dec 30 '19 at 20:33
  • The reason we still see this terrible syntax today is because people see is as simple and easy to learn and they never learn how bad the form is. Let's not teach people bad code just because it appears easy to learn. Let's teach newbies the right way to code. As you'll see from linked post, there are very real reasons this syntax should never be used. Ever. – Scott Marcus Dec 30 '19 at 20:43
  • I did check your post and even read the MDN doc linked to it. You have a valid point but I still believe that you should learn all the possible ways. There might be some old ways that you may forget but a common thing like this must be (at the very least) known. – Utsav Barnwal Dec 30 '19 at 20:51
  • The reason it's so common is because it's still being copy/pasted and shown to people without an understanding of what it actually does. If people were educated about it, it would never get used. Do yourself (and others a favor) and forget that this approach exists. – Scott Marcus Dec 30 '19 at 20:55
  • Agreed but still, I stand at my point "it should be known" as it can come really handy at times. – Utsav Barnwal Dec 30 '19 at 21:02
  • There is not one situation where it would come in handy. It is a determent in every use case. Your profile indicates that you are a new programmer who is interested in learning and teaching. Take it from this professional IT trainer and developer of over 25 years, there are many legacy techniques that still "work" (or appear to) when it comes to web development, but those techniques lead to inferior code and don't follow modern standards and methodologies. You aren't serving yourself or others well by advocating them. – Scott Marcus Dec 30 '19 at 21:26
  • Undoubtedly you're much more experienced and qualified than I am and I respect it. As I have mentioned earlier, I agree with your point but I would rather know inline HTML event attributes than not knowing them and would like the same for others too. – Utsav Barnwal Dec 30 '19 at 21:36
  • Please don't attempt to edit the functionality of other people's answers. Edits should be used to correct typos, fix formatting, or make an answer more clear, but changing the code in someone else's answer so that it functions differently than how the person who posted it intended is not appropriate. In those cases, add a comment or post your own answer. – Scott Marcus Mar 06 '20 at 22:18