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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question on using Var's

    so my issue is this, I have the following script:

    Code:
    function tax(){
    		
    		var materialtotal = document.getElementById( 'materialtotal' ).value;
    		var materialcost = document.getElementById( 'materialcost' ).value;
    		var shipping = document.getElementById( 'shipping' ).value;
    		var salestax = Math.round(((materialcost / 100) * 8.1)*100)/100;
    		var materialtotal = (materialcost * 1) + (salestax * 1) + (shipping * 1);
    		
    		
    		document.getElementById( 'materialcost' ).value = materialcost; 
    		document.getElementById( 'salestax' ).value = salestax;
    		document.getElementById( 'shipping' ).value = shipping;
    		document.getElementById( 'materialtotal' ).value = materialtotal;
    		
    		
      
    }
    Basically this code will take the values of materialcost add it to the values of salestax then add the shipping (if any) to the value and populate the materialtotal field. The script works fine for that purpose but my issue comes when I know need to take the value of the materialtotal field and add it to the value of the labortotal field and populate that sum in the grandtotal field.

    I tried something like this:

    Code:
    function tax(){
    		var grandtotal = document.getElementById( 'grandtotal').value;
    		var labortotal = document.getElemnetById( 'labortotal').value;
    		var materialtotal = document.getElementById( 'materialtotal' ).value;
    		var materialcost = document.getElementById( 'materialcost' ).value;
    		var shipping = document.getElementById( 'shipping' ).value;
    		var salestax = Math.round(((materialcost / 100) * 8.1)*100)/100;
    		var materialtotal = (materialcost * 1) + (salestax * 1) + (shipping * 1);
    		var grandtotal = (labortotal * 1) + (materialtotal * 1);
    		
    		document.getElementById( 'materialcost' ).value = materialcost; 
    		document.getElementById( 'salestax' ).value = salestax;
    		document.getElementById( 'shipping' ).value = shipping;
    		document.getElementById( 'materialtotal' ).value = materialtotal;
    		document.getElementById( 'grandtotal' ).value = grandtotal;
    		document.getElementById( 'labortotal' ).value = labortotal;
    		
      
    }
    but when I add the onPropertychange=tax() to the materialtotal id tag it locks up the browser, (I'm aware onPropertychange is specific to IE) is this because I have something wrong in the code or because I can't use the materialtotal in the second var since it already used before that?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,609
    Thanks
    0
    Thanked 645 Times in 635 Posts
    It doesn't matter how many times you put var in front of a statement - all the variables are declared exactly once at the very start of the function before the first statement inside the function is run.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Code:
    onPropertychange=tax()
    should be

    Code:
    document.getElementById( 'materialtotal' ).onchange=tax;
    as an side for future time savings and easier readability, consider re-using common functionality instead of repeating yourself:
    Code:
    function tax(){
    		function el(id){return document.getElementById(id);}
    
    		var materialtotal = el( 'materialtotal' ).value;
    		var materialcost = el( 'materialcost' ).value;
    		var shipping = el( 'shipping' ).value;
    		var salestax = Math.round(((materialcost / 100) * 8.1)*100)/100;
    		var materialtotal = (materialcost * 1) + (salestax * 1) + (shipping * 1);
    		
    		
    		el( 'materialcost' ).value = materialcost; 
    		el( 'salestax' ).value = salestax;
    		el( 'shipping' ).value = shipping;
    		el( 'materialtotal' ).value = materialtotal;
    		
    		
      
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

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