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 ₹ <?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 ₹ <?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: ₹ <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 🛒</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 echo
s 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?