View Full Version : Javascript anyone?
welby
19-08-2010, 04:53 PM
Hi, I am designing a new form and have hit a snag.
I have three fields;
Start Time
End Time
Rate
I want to have a calculation like this;
(End Time - Start Time) x (Rate) = Total
so if you start at 5pm and end at 10pm and your rate is £10 the total should equal £50.
I want to do this on a submit button so I am using the onclick method as:
onclick="document.getElementById('total').value = parseInt(document.getElementById('End').value) - parseInt(document.getElementById('Start').value)"
This does the first part of the calculation (End Time - Start Time) - but I have tried to add the third element (the rate) and it does not work correctly.
Any of you javascript kids have any ideas?
Thanks in advance
mirepup
19-08-2010, 11:29 PM
It's the currency symbol messing you up.
parseInt won't handle a string that starts with a non-numeric value.
Try this:
<script>
function calcRate(){
start = parseInt(document.getElementById("Start").value);
end = parseInt(document.getElementById("End").value);
rate = document.getElementById("Rate").value;
newRate = parseInt(rate.replace(/D/g, ""));
total = (end-start) * newRate;
alert( total );
}
</script>
onclick = "javascript:calcRate();"
NOTE! In the replace, it should say /\ before the D, but the editor here is stripping it.
I'd also caution that this (as it is) is a pretty dangerous piece of code, prone to breaking under all but ideal situations.
use DJEP .... its fantastic :)
Rowleys
20-08-2010, 07:27 AM
I'm looking at doing somthink like this on my site...
Please can you send me the full coding ?? with the forum boxes if you can
Marc J
20-08-2010, 07:32 AM
http://www.codingforums.com/showthread.php?t=156394
Rowleys
20-08-2010, 08:14 AM
Just having a play with that coding but not idea how it work's lol
i need something that the first 2 hour is £100 then every hour after that is £30
welby
20-08-2010, 09:04 AM
It's the currency symbol messing you up.
parseInt won't handle a string that starts with a non-numeric value.
Try this:
NOTE! In the replace, it should say / before the D, but the editor here is stripping it.
I'd also caution that this (as it is) is a pretty dangerous piece of code, prone to breaking under all but ideal situations.
Hi thanks for the reply.
I wasnt using a pound symbol in my calculation as I put the symbol in a label so that I was parsing just a numeric value so I guess the code should be:
<script>
function calcRate(){
start = parseInt(document.getElementById("Start").value);
end = parseInt(document.getElementById("End").value);
rate = document.getElementById("Rate").value;
total = (end-start) * Rate;
alert( total );
}
</script>
onclick = "javascript:calcRate();"
Nice to know someone else likes coding...:beer1:
welby
20-08-2010, 09:07 AM
use DJEP .... its fantastic :)
what is DJEP?
http://www.singsurf.org/djep/index.php
???
Vectis
20-08-2010, 09:23 AM
what is DJEP?
http://www.singsurf.org/djep/index.php
???
Niet.
http://www.djeventplanner.com
welby
20-08-2010, 09:40 AM
Niet.
http://www.djeventplanner.com
aha - I thought so - but I am not paying out $99 or whatever just so that I can provide a simple quote form - I have easy estimates as recommended by yourself but can't seem to do it in that either! lol
I have been looking at different form systems for the joomla cms and when it comes to calculations of this type they run a mile!
Thanks for your help.
DeckstarDeluxe
20-08-2010, 09:49 AM
aha - I thought so - but I am not paying out $99 or whatever just so that I can provide a simple quote form - I have easy estimates as recommended by yourself but can't seem to do it in that either! lol
I have been looking at different form systems for the joomla cms and when it comes to calculations of this type they run a mile!
Thanks for your help.
I pay £8 a month and it has an instant quote function plus other benefits.
welby
20-08-2010, 09:59 AM
Well,
I'm making progress
I now have an alert that is showing the correct amount but I need to post the value back and not show an alert:
Heres my working code:
<script>
function calcRate(){
start = parseInt(document.getElementById("Start").value);
end = parseInt(document.getElementById("End").value);
rate = parseInt(document.getElementById("Rate").value);
total = (end-start) * rate;
alert( total );
}
</script>
welby
20-08-2010, 10:31 AM
I pay £8 a month and it has an instant quote function plus other benefits.
so that's £96 a year?
mirepup
20-08-2010, 11:20 PM
Hey Welby, what's the ID of the field you want the total in?
Simply remove the alert( total ); line and replace it with
document.getElementById("Total").value = total
Powered by vBulletin® Version 4.2.0 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.