Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 08-15-2007, 11:25 AM PM User | #1 tpeck Regular Coder     Join Date: Oct 2002 Location: Sydney, Australia Posts: 802 Thanks: 42 Thanked 6 Times in 5 Posts javascript addition Hi, I am trying to automate some addition and multiplication. [no.1] + [no.2] = [total 1] [no.3] + [no.4] = [total 2] [no.5] + [no.6] = [total 3] [no.7] + [no.8] = [total 4] subtotal 1 = [total 1 + total 2 + total 3 + total 4] subtotal 2 = [subtotal 1 divided by 16] TOTAL = [subtotal 2 multiplied by 10] Ideally, I would have the user enter the nos. 1 to 8 and hit a single button to get totals 1 to 4, the two subtotals, and the TOTAL showing in input boxes. Before I attempt this mini project, is it possible do you think? Or does it have to be done by hitting more than one button? I have no idea where to start. Happy to pay someone to get this totals thing finished. Three years on this major project and I have to get it all done by next month. Thanks. __________________ The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
 08-15-2007, 11:54 AM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts Is this what you require? Code: ```

Number 1

Number 2

Total 1

Number 3

Number 4

Total 2

Number 5

Number 6

Total 3

Number 7

Number 8

Total 4

SubTotal1

SubTotal2

GRAND TOTAL

``` You may want to add checks to ensure that the user enters (valid) numbers in the boxes (i.e. valid range, not negative etc.) E.G:- function checkvalid(which) { if (isNaN(which.value) || (which.value <0) || (which.value > 99)) { alert ("Invalid entry"); which.value = ""; which.focus(); } } Last edited by Philip M; 08-15-2007 at 12:29 PM..
 08-15-2007, 12:01 PM PM User | #3 mcjwb Regular Coder   Join Date: Jul 2007 Location: UK Posts: 223 Thanks: 0 Thanked 14 Times in 14 Posts How about this: Code: ``` New Document
+=
+=
+=
+=
subtotal1
subtotal2
total: ``` __________________ Javascript Debugging Tools: IE Script Debugger | IE Developer Toolbar | Fiddler || FF Firebug | FF Web Developer
 08-15-2007, 01:47 PM PM User | #4 tpeck Regular Coder     Join Date: Oct 2002 Location: Sydney, Australia Posts: 802 Thanks: 42 Thanked 6 Times in 5 Posts That is just perfect - both ideas work and so logical too! Thanks so much. My total now ends up as a figure often with a decimal remainder which I need to get rid of (and the decimal point). I can truncate the figure to three items with: frm.total.value = (subTotal/16 * 10).toString().substr(0,3); ...but if the number is less than 99 it includes the decimal point. How do I convert, say, 145.564 to 145 only and make it work similarly for all numbers? __________________ The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
 08-15-2007, 01:58 PM PM User | #5 tpeck Regular Coder     Join Date: Oct 2002 Location: Sydney, Australia Posts: 802 Thanks: 42 Thanked 6 Times in 5 Posts WAIT! Sorry I have figured it out... frm.total.value = Math.round(subTotal/16 * 10); __________________ The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
 08-15-2007, 02:01 PM PM User | #6 mcjwb Regular Coder   Join Date: Jul 2007 Location: UK Posts: 223 Thanks: 0 Thanked 14 Times in 14 Posts Nice to see you updated your post Phil! Glad you got it working tpeck. __________________ Javascript Debugging Tools: IE Script Debugger | IE Developer Toolbar | Fiddler || FF Firebug | FF Web Developer
08-15-2007, 03:47 PM   PM User | #7
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,473
Thanks: 200
Thanked 2,469 Times in 2,447 Posts
Quote:
 Originally Posted by tpeck How do I convert, say, 145.564 to 145 only and make it work similarly for all numbers? WAIT! Sorry I have figured it out... frm.total.value = Math.round(subTotal/16 * 10);
Math.round will round up 145.564 to 146. If you want to round down use Math.floor

or:-

frm.total.value = (subTotal/16 *10).toFixed(0);

 08-17-2007, 06:24 AM PM User | #8 tpeck Regular Coder     Join Date: Oct 2002 Location: Sydney, Australia Posts: 802 Thanks: 42 Thanked 6 Times in 5 Posts I am trying to take this one step further by having the number boxes automatically entered from values existing in input boxes on an iFrame on the same page. (People are complaining about entering the numbers when they already exist on the page.) I've tried lots of things like below, but they dont work - yet: Code: ```function calcTotals(frm){ frm.no1.value = document.iframe['I2'].getElementsById('a1ap.value');``` The iFrame is called 'I2' and the input box containing the value for the first number box ('no.1') is called 'a1ap'. I've made a demo at http://aapress.com.au/demo/iframes/p1.html Where am I going wrong? If I can get the values to show onload in the number boxes as well as in the iFrame, I think also calculating the totals onload - as shown in this post -will display the FINAL TOTAL automatically. __________________ The difference between genius and stupidity is that genius has its limits. (Albert Einstein) Last edited by tpeck; 08-17-2007 at 06:51 AM..
 08-17-2007, 10:21 AM PM User | #9 mcjwb Regular Coder   Join Date: Jul 2007 Location: UK Posts: 223 Thanks: 0 Thanked 14 Times in 14 Posts You've made a couple of mistakes there; Frames and iframes are referenced through the document.frames collection (there is no "iframe" collection). There is no method getElementsById, it's getElementById (typeo perhaps?). Once you've referneced the frame/iframe you have actually got a reference similar to window, so you have to make a reference to the document within the frame (if you want to use something like getElementById). You can only put the id of an element in the arguement of getElementById, you need to move the '.value' bit outside. This all means you need something like this (in p1.html): Code: ```function load(){ if(document.frames['I3'] && document.frames['I2']){ document.frames['I3'].document.getElementById('no1').value = document.frames['I2'].document.getElementById('a1ap').value; } }``` then your body should be , but I suspect you know this bit! If you want to put the javascript in a different file it'll need modifying, let me know if this is the case and you need any help. Hope that helps. __________________ Javascript Debugging Tools: IE Script Debugger | IE Developer Toolbar | Fiddler || FF Firebug | FF Web Developer Last edited by mcjwb; 08-17-2007 at 10:23 AM..

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:10 AM.