0

So I have a fairly simple task at hand. Read the values from a Radio button and a select box, multiply them and show them in a span tag.

The problem is, I can't read both simultaneously. If I read one, the other doesn't send data through.

Here's my HTML:

<form class="" action="" method="post">
  <div class="form-group" required oninvalid="this.setCustomValidity('Please select a quanitity')" oninput="this.setCustomValidity('')">
    <label for="">Select purchase quantity:</label><br>
    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="qty" id="bachelorsRadio" value="<?php echo $price125; ?>">
      <label class="form-check-label" for="bachelorsRadio">125 grams for &#8377; <?php echo $price125; ?></label>
    </div>
    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="qty" id="mastersRadio" value="<?php echo $price250; ?>">
      <label class="form-check-label" for="mastersRadio">250 grams for &#8377; <?php echo $price250; ?></label>
    </div>
  </div>
    <div class="form-group col-md-6">
      <label for="orderquantity">Select packet quanitity:</label>
      <select class="custom-select"  id="myselect">
        <option selected>Select packet quantity</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>
    </div>
    <div class="col-6">
      <p> Total price: &#8377; <span class="price">0</span> </p>
    </div>
  <button type="button" class="btn col-4 btn-outline-success">Buy now!</button>
  <button type="button" class="btn col-4 btn-outline-warning">Add to cart &#x1F6D2;</button>
</form>
});

Here's my JQuery:

$(document).ready(function(){
  var amt = $('input[type=radio]:checked').val();
  var packet = $("#myselect option:selected").val();
  var final = amt*packet;
  $('.price').html(final);

I can only get either the radio button's value or the select box's value to show up in my price span. Also, this needs to happen in real-time, so the values need to change if there are any changes in the text field as well.

Also in case, you were wondering the PHP echos are values I've pulled from a MySQL database and stored in variables. They're showing up just fine.

How do I get the values of the radio button and the select box, multiply them and show them in the price span?

YaddyVirus
  • 301
  • 4
  • 21

1 Answers1

0

You can try the following code:

function calculateFinal() {
  // Get the two values and parse them to numbers.
  // You can use `parseInt()` if you're only expecting integers.
  var amt = parseFloat($('input[name="qty"]:checked').val());
  var packet = parseFloat($("#myselect option:selected").val());

  // Multiply the values:
  var final = amt*packet;

  // Set the value using `.text()` as we're not adding any HTML.
  // `.text()` is safer than `.html()` as it protects against possible XSS.
  $('.price').text(final);
}

// Perform the calculation once the page has loaded:
$(calculateFinal);

// Listen to the change events on the radio buttons and the select:
$("input[name='qty'], #myselect").on("change", calculateFinal);