Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  200] [ 5]  / answers: 1 / hits: 16864  / 10 Years ago, fri, october 17, 2014, 12:00:00

I want to calculate the value of the 1st 2 text boxes without a command button
Example i have 3 text boxes.



The first 2, where the numbers will be inputed and the last 1 will be the sum or product and so on.
Now i want it to auto compute.
For example i have inputed values 2 and 3 on the 1st 2 text boxes then automatically the sum or product or whatever result will be displayed on the 3rd text box.
How am i able to do this? Thanks



    <html>
<head>
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>
<script>
$('#texttwo').keyup(function(){
var textone;
var texttwo;
textone = parseFloat($('#textone').val());
texttwo = parseFloat($('#texttwo').val());
var result = textone + texttwo;
$('#result').val(result.toFixed(2));
});
</script>
</head>
<body>
<input type=text name=value1 id=textone>
<input type=text name=value2 id=texttwo>
<input type=text name=result id=result>
</body>
</head>

More From » jquery

 Answers
10

You can achieve this by using jQuery.
Include jQuery in your project by putting this in your <head>



<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>


Then, at the end of the file:



<script>
$('#texttwo').keyup(function(){
var textone;
var texttwo;
textone = parseFloat($('#textone').val());
texttwo = parseFloat($('#texttwo').val());
var result = textone + texttwo;
$('#result').val(result.toFixed(2));


});
</script>


This will give you the result when ever you change the value of the second box.



You will need to do this, too:



Change



    <input type=text name=value1>
<input type=text name=value2>
<input type=text name=result>


to:



    <input type=text name=value1 id=textone>
<input type=text name=value2 id=texttwo>
<input type=text name=result id=result>


EDIT



This is my entire file:



<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>


<input type=text name=value1 id=textone>
<input type=text name=value2 id=texttwo>
<input type=text name=result id=result>

<script>
$('#texttwo').keyup(function(){
var textone;
var texttwo;
textone = parseFloat($('#textone').val());
texttwo = parseFloat($('#texttwo').val());
var result = textone + texttwo;
$('#result').val(result.toFixed(2));


});
</script>

[#69095] Tuesday, October 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mustafaericho

Total Points: 322
Total Questions: 103
Total Answers: 110

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mustafaericho questions
Mon, May 31, 21, 00:00, 3 Years ago
Sun, May 23, 21, 00:00, 3 Years ago
Sat, Feb 13, 21, 00:00, 3 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
;