/    Sign up×
Community /Pin to ProfileBookmark

autocalculate and documenrt.getElementById Problem in combination

Good day!

This code is for autocalculate, and I got a problem when i want ot add the output of autocalculate in the value of textbox Amount.

here is the code:

[code=php]
<script type=”text/javascript” language=”javascript”>

function autocalearn(oText)
{
if (isNaN(oText.value)) //filter input
{
alert(‘Numbers only!’);
oText.value = ”;
}
var field, val, oForm = oText.form, TotEarn = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
TotEarn += val; //accumulate
}
}
//oForm.TotEarn.value = TotEarn.toFixed(2); //out
oForm.TotEarn.value = TotEarn.toFixed(2) + document.getElementById(‘Amount’).value;
}

</script>
[/code]

the output of this code is when I input 10 and the amount data is 100 the total earn is 10100..its wrong..I want result is 110.

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 17.2011 — [B]toFixed()[/B] method returns a String primitive, not a Number. Same with the [B]value[/B] attribute. As a result, you have performed a concatenation there, not a Math add.

You should transform, first, the Strings into Numbers:
<i>
</i>oForm.TotEarn.value = [COLOR="Blue"]Number([/COLOR]TotEarn.toFixed(2)[COLOR="Blue"])[/COLOR] + [COLOR="Blue"]Number([/COLOR]document.getElementById('Amount').value[COLOR="Blue"])[/COLOR];
Copy linkTweet thisAlerts:
@pactor21Oct 17.2011 — document.getElementById('Amount').value might be a still string, and you thought you had your code add TotEarn.toFixed(2), which is a number for sure, to document.getElementById('Amount').value.

However what actually happened was javascript converted TotEarn.toFixed(2) to a string, and concatenated it to document.getElementById('Amount').value.

That's why you got 10100, which is a string concatenation between '10' and '100'.

So if you want to actual addition instead of concatenation, you have to turn document.getElementById('Amount').value into a number the same way you did to TotEarn.

Well Kor beat me, and I didn't know toFixed(2) returns a string.
Copy linkTweet thisAlerts:
@rhodaroseauthorOct 17.2011 — Thank you so much it works...

Is it automatically round into two decimal places?

Again Thank you..
Copy linkTweet thisAlerts:
@rhodaroseauthorOct 17.2011 — I test the code and I found out I got a problem in rounding into two decimal places. in php i used round, how about in javascript.


Thank you...
Copy linkTweet thisAlerts:
@KorOct 17.2011 — I test the code and I found out I got a problem in rounding into two decimal places[/QUOTE]
[I]Which[/I] problem? Can you detail, please?
Copy linkTweet thisAlerts:
@rhodaroseauthorOct 17.2011 — The problem is when I input a number with decimal it has a problem in decimal places. I want is it will be in to two decimal places. Thank you


Like for example the Amount is 3884.34 and I input in the textboxes the ff: 10.50, 10.58, 10.01 when I input 10.01 the output become 3915.43000000003. I want it to round it off into two decimal places.

Thank you
Copy linkTweet thisAlerts:
@KorOct 17.2011 — use toFixed(2) to round the[I] final[/I] value.
Copy linkTweet thisAlerts:
@rhodaroseauthorOct 17.2011 — The final value is the code that you given and I don't know how can I round it off using toFixed(2).
Copy linkTweet thisAlerts:
@rhodaroseauthorOct 17.2011 — I tried everything but I failed to convert it to two decimal places...Thanks
Copy linkTweet thisAlerts:
@KorOct 17.2011 — C'mon...?
<i>
</i>var tot=Number(TotEarn) + Number(document.getElementById('Amount').value);
oForm.TotEarn.value = tot.toFixed(2);
Copy linkTweet thisAlerts:
@rhodaroseauthorOct 17.2011 — Thank you so much....?
Copy linkTweet thisAlerts:
@newphpcoderOct 20.2011 — Good day!

the only problem now is when I add data in total earn it did not add in Over all total.. the only good happen is when I add deductions it takes effect or subtract in over all total..

Like for example I add total earn from 1000 i add 20 the total earn become 1020 but the overall total still 100 but when i add also deduction from 100 I add 10 the overall total become 910 which is correct. I only want is when the total earn was change also the overall total will also change..

Thank you

here is the revise code:
[CODE]
<script type="text/javascript">
var tot;
function autocalearn(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotEarn = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
TotEarn += val; //accumulate
}
}

tot=Number(TotEarn) + Number(document.getElementById('Amount').value);
oForm.TotEarn.value = tot.toFixed(2);


}

</script>


<script type="text/javascript">

function autocalded(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, TotalDed = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
TotalDed += val; //accumulate
}
}


var totded=Number(TotalDed) + Number(document.getElementById('Deductions').value);
oForm.TotalDed.value = totded.toFixed(2);
var overallTotal = tot - totded;

oForm.TakeHomePay.value = overallTotal.toFixed(2);
}

</script>
[/CODE]


Thank you
×

Success!

Help @rhodarose spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...