I am new user at JS and I am trying to sum 2 values from HTML input.
The problem: I have 3 HTML input's:
- Input by
type=number(select number from input field); - 2 inputs with
type=radio
I want to sum the value of the number field with value of the selected radio button in real time (using jQuery).
JS:
$('#quantity').click(function () {
$('#sum').text($(this).val());
});
$('#y').click(function () {
var y = document.querySelector('#y').value;
$('#sum').text($(this).val()).y;
});
$('#x').click(function () {
var x = document.querySelector('#x').value;
$('#sum').text($(this).val()).x;
});
HTML:
<form>
<input id="quantity" type="number">
<input id="y" name="math" type="radio" value="0">
<input id="x" name="math" type="radio" value="1">
</form>
<span id="sum"></span>
I just want to add the value of the selected radio input to the value in the "quantity" field.
Example: If the value of the "quantity" field is "100" and I click on the "x" radio button (value = 1), then print "101".
xandyare not jQuery properties, you want$("#sum").text($(this).val()+x);. Also, usegetElementByIdinstead ofquerySelectorwhen you are grabbing just an ID. Also, you can usethis.valueinstead of$(this).val()since it appears you're mixing native DOM and jQuery.