Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Location
    austin, tx
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Grand Total Javascript Math

    Here is my current page . "View Source" is going to work a lot better than me pasting the code here because there is a ton of javascript in this page. I have added a Grand Total box at the bottom right of the page.
    Code:
    function grandtotal() {
    document.quoteform.GrandTotal.value = "$" + round(document.quoteform.Extension.value + document.quoteform.Extension2.value + document.quoteform.Extension3.value + document.quoteform.Extension4.value + document.quoteform.Extension5.value)}
    The above code is running when ' onBlur="grandtotal()" ' after each "Extension" field. My problem is that after the "$" has been added to the prior fields, the javascript math function won't work. I can obviously rewrite the code and make a GrandTotal() for every time you add a new set of Qty and Price, but my script is already inefficient as is, so I'm trying to think of a workaround.

    Maybe I should start from scratch...but is there a way to do what I'm doing is a much more effecient way? ...ok, I'm sure most of my scripting is lame, but it works, so I'm specifically referring to the math functions. any ideas are appreciated.

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function update(quantity,price,extension,total)

    Here is some code I wrote for this purpose:

    Code:
    String.prototype.reverse=function(){return this.split('').reverse().join('')};
    function number(d){ return d.match(/(\-|\d|\.)/gi).join(''); };
    function dollar(n){
      var s=(n+'.00').split('.');
      s[0]=((s[0]+'').reverse().match(/\d{1,3}\-?/gi)+'').reverse(); //greedy RegExp gathers groups of 3 digits
      s[1]=(s[1]+'00').slice(0,2);
      return("$"+s[0]+'.'+s[1]);
    };
    function update(quantity,price,extension,total){
    	with(total)value=0;
    	for(var i=0;i<extension.length;i++){
    		with(quantity[i])value=number(value);
    		with(extension[i]){
    			value=quantity[i].value*number(price[i].value);
    			total.value-= -value;
    			value=dollar(value);
    		};
    		with(price[i])value=dollar(value);
    	};
    	with(total)value=dollar(value);
    };
    Then just rely upon like named fields acting as an array...

    Code:
    <input name="quantity" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="price" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="extension" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />
    <input name="quantity" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="price" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="extension" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />
    <input name="quantity" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="price" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><input name="extension" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />
    <input name="total" value="0" onblur="with(this.form)update(quantity,price,extension,total);void(0)" /><br />
    Enjoy.

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    austin, tx
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the formula works great, and i started with something similar in the sense that i wouldn't have to name each input differently in order to do the math, but my problem is that many times in my form, entire lines get left blank, and the blank fields do not get sent thru the email. my sendmail is a huge PHP with lots of formatting (type your own email into the "Cust. Email" field to send yourself an example) so appearance is everything. with the script you provided as-is, it fills in every field and thus causes zeros and "$" to appear in the sent email which is not what i desire.

    that being said, is there any way to integrate the part of your script that adds the $ to what I currently have?

    please let me know if my questions don't make any sense...i'm trying to explain as best i know how.

  • #4
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    You could break out of the loop, if a blank is found.

    You could break out of the loop, if a blank is found.

    In the update function...

    Code:
    if (quantity[i].value == '') break;
    if (price[i].value == '') break;

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    austin, tx
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, i'm moving to a test page now www.jasonpcarroll.com/allcomponents/test.html.

    I can't quite figure out where to put the script you just gave me. When I simply plugged it in where you said, it didn't do anything. Any ideas? Or can you plug it in the full formula for me?

    Edit: Ok, I just took out the default value of zero and it works the way i want it to without adding anything (duh, that was easy), however, it doesn't add the $ to the grand total until the very end once every field is filled in, but again, every field won't be filled in most of the time. Check out the sample page.


    ...edit #2...in I.E. it's actually telling me there's an error with this method of leaving out the default value.
    Last edited by sftl99; 07-06-2005 at 12:49 AM.

  • #6
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Use this update function instead...

    Use this update function instead...

    Code:
    function update(quantity,price,extension,total){
    	with(total)value=0;
    	for(var i=0;i<extension.length;i++){
    		if(!(quantity[i].value=='' || price[i].value=='')){
    			with(quantity[i])value=number(value);
    			with(extension[i]){
    				value=quantity[i].value*number(price[i].value);
    				total.value-= -value;
    				value=dollar(value);
    			};
    			with(price[i])value=dollar(value);
    		}
    	};
    	with(total)value=dollar(value);
    };

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    austin, tx
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that's beautiful! thank you. now i just need to redo some code! i appreciate the help big time.

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    austin, tx
    Posts
    148
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edit: For anyone who looked at this last problem I had and worked even one second on it, I deeply apologize. My error was in Dreamweaver adding code that I didn't want. It's fixed now, so sorry!
    Last edited by sftl99; 07-06-2005 at 03:39 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •