I enter several values in the Input (s). The sum goes there, I need this sum to appear in another inpute, so that later I could use it. For example 2 + 5, the seven should appear in another input with an id for example S, so that you can further access this value
- show chtoli attempts to solve - Duck Learns to Take Cover
|
2 answers
Use the oninput event:
function updateValues() { var $ = document.querySelector.bind(document); //теперь можно использовать "$(sel)" вместо "document.querySelector(sel)" $('#out').value = (+$('#in1').value || 0) + (+$('#in2').value || 0); // "+value" - преобразование в число // "|| 0" - преобразование 0,NaN,underfined,null в 0 } // добавление из js var $ = document.querySelector.bind(document); $('#in2').addEventListener('input', updateValues); <!- vv- добавление в HTML -vv -> <input id="in1" oninput="updateValues();" value=0> <br> <input id="in2" value=0> <br> <br> <input id="out" value=0> - Report number <input type = "text" name = "kolnarap" id = "kolnarap" placeholder = "Enter number" value = "0"> <br> <br> - Big Daddy
- Is it possible this code, only on JS?, Without jquery - Big Daddy
|
You can listen to kepress from both input'ov, in which you add the terms and in the handler to update the amount in input'e S.
$("#S1").on("keypress", function() { $("S").val(parseFloat($(this).val()) + parseFloat($("#S2"))); }); $("#S2").on("keypress", function() { $("S").val(parseFloat($(this).val()) + parseFloat($("#S1"))); }); |