www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: adding calculation

  1. #1
    Join Date
    Oct 2007
    Posts
    21

    adding calculation

    Hi,

    I have a form which I need add together a number from a previous calculation with a number that the user inputs but I can't seem to get CapEx recognised as a number! Can anyone help me please! I have put the code below:



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script></script>



    <script type="text/javascript">
    <!--
    function resettoggle() {
    var e = document.getElementById('foo');
    e.style.display = 'none';
    }

    function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'none')
    e.style.display = 'block';
    else
    e.style.display = 'none';
    }
    //-->

    </script>
    </head>
    <body onLoad="resettoggle()">

    <tr>
    <td><div class="fieldlabel">Change Title:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(1requestfctitle)" rows="2" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Requested By:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(2requestfcrequestor)" rows="1" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">HOTB:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(3requestfcHOTB)" rows="1" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Change Description:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(4requestfcdesc)" rows="3" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Required By Date:</div></td>
    <td>
    <a href="#" onClick="cal1.select(document.forms[0]['userstringvalue(5requestfcrequired)'],'anchor1','dd/MM/yy'); return false;" NAME="anchor1" ID="anchor1">
    <img src="/assystEnterprise/shared/images/calendar.gif" border="0" hspace="0" vspace="0">
    </a>
    </td>
    <td><textarea name="userstringvalue(5requestfcrequired)" rows="1" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Total Man Days:</div></td>
    <td></td>
    <td><textarea name="manDays" rows="1" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>

    <INPUT TYPE="button" NAME="DoIt" VALUE="calculate resource cost" OnClick="Resource.value= '' +(manDays.value * 400)"><br />

    <tr>
    <td>
    <INPUT TYPE="text" NAME="Resource" Size="16">
    <br>
    </td>
    </tr>

    <tr>
    <td><div class="fieldlabel">CapEx Cost ():</div></td>
    <td></td>
    <td><textarea name="CapEx" rows="1" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>

    <INPUT TYPE="button" NAME="DoIt" VALUE="total cost of change" onclick="Total.value= '' + Number(Resource.value.replace(//,'')) + Number(CapEx.value)"><br />

    <tr>
    <td>
    <INPUT TYPE="text" NAME="Total" Size="16">
    <br>
    </td>
    </tr>


    <a href="#" onclick="toggle_visibility('foo');">Click here if the Total Cost of Change is less than &pound;5k</a><br />



    <div id="foo">
    <tr>
    <td colspan="5"><h3>If the Total Cost of Change is less than 5k, please complete the following fields...</h3></td>
    <td></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Reasons for Change:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(8requestfcreasons)" rows="3" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Known RIDAs:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(9requestfcrida)" rows="3" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Resource Requirements:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(10requestfcresource)" rows="3" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>
    <tr>
    <td><div class="fieldlabel">Additional Info:</div></td>
    <td></td>
    <td><textarea name="userstringvalue(11requestfcaddinfo)" rows="2" class="datafield"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>

    <tr><td colspan="5"><hr/></td></tr>

    <tr>
    <td colspan="5">
    </div>
    <br />

    <tr>
    <td colspan="56"><h3>If the Total Cost of Change is more than 5k, please attach a Project Brief to this request.<br><a title="Project Brief" href="file:///S:/!TBPM%20Templates/1.%20Selecting%20Business%20Change/Project%20Brief%20Template%20v1.0.doc" target="_blank">Click here</a> to view the Project Brief template from the <a title="Processes and Controls" href="http://nrfour/files/Intranet%20Spec%20V3.htm" target="_blank">Processes & Controls area</a> of the intranet.</h3></td>
    <td></td>
    </tr>
    <br />
    <input type="submit" name="LogEvent" value="Submit" onclick="setDispatchTarget('logEvent');" class="button">

    </body>
    </html>

  2. #2
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    1. I don't see any javascript functions other then the toggle visibility ones
    2. If you use [code] tags it is much easier to read
    3. There is no need for table based layouts
    4. Why are you using XHTML if your not going to a) write valid XHTML and b) not going to serve it as application/xhtml-xml?

    Now that I said that, post some of the javascript functions and maybe we can see whats up.

  3. #3
    Join Date
    Oct 2007
    Posts
    21
    ok I'm new to all of this so apologies!

    I was given this code to modify and I believe the form sits within a jsp page! The total man days calc. works fine but I can't seem to get a total when I try to add the two vaues together! see below! It doesn't seem to add it as a number? I'm now off to cry!
    [code]
    <tr>
    <td><div class="fieldlabel">Total Man Days:</div></td>
    <td></td>
    <td><textarea name="manDays" rows="1" class="datafield" onBlur="Resource.value= '' +(manDays.value * 400)"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>
    </tr>





    <tr>
    <td>
    <INPUT TYPE="text" NAME="Resource" Size="16">
    <br>
    </td>
    </tr>

    <tr>
    <td><div class="fieldlabel">CapEx Cost ():</div></td>
    <td></td>
    <td><textarea name="CapEx" rows="1" class="datafield" onBlur="Total.value= '' +(Resource.value.replace(//,'')) + Number(CapEx.value)"></textarea></td>
    <td></td>
    <td><div class="errormsg"></div></td>

    <td>
    <p>total cost
    <INPUT TYPE="text" NAME="Total" Size="16">
    <br>
    </td>
    </tr>

  4. #4
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Its ok I'm just letting you know for future reference, so you know this is not the right way to accomplish this. Also, when using [code], you need a [ / code] (no spaces). Here's how I would do it.
    PHP Code:
    //all this needs to be in <script> tags at the top
    function setResource (val) {
       var 
    resource document.getElementById("resource"); //get the input field
       
    val *= 400//multiply the value by 400;

       
    resource.value "$" +val//set the value
      //dont know how to make a pound sign lol
    }

    function 
    setTotal(val) {
      var 
    resourceVal document.getElementById("resource").value//get the value
      
    var total document.getElemetnById("total");

      
    val val.replace(/$/, ''); //remove the "pound" sign
      
    val parsefloat(val//convert to a floating point number (with decimals)
                                 //if its only integers, you can use "parseint(val)"

      
    val += resourceVal//add the values

      
    total.value '$' +val;  

    Then change the following in your HTML:
    HTML Code:
    <textarea name="manDays" rows="1" class="datafield" onblur="setResource(this.value)"></textarea>
    
    <!-- xhtml requires all lowercase letters and the use of "id" instead of "name"-->
    <input type="text" id="resource" size="16">
    
    <textarea name="CapEx" rows="1" class="datafield" onblur="setTotal(this.value)"></textarea>
    
    <input type="text" id="total" size="16">
    There are much better ways for this whole thing to be set up, but since you got handed a mess I figured I'd just show you how I would make it work.
    Last edited by TJ111; 11-01-2007 at 10:10 AM.

  5. #5
    Join Date
    Oct 2007
    Posts
    21

    errors appear

    Hi thanks for all your help. I have copied your code but it still doesn't calculate a total! It refers to this line of code

    Code:
      var total = document.getElemetnById("total");
    here is my code

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script></script>
    
    
    
    <script type="text/javascript">
    <!--
    function resettoggle() {
    var e = document.getElementById('extraFields');
    e.style.display = 'none';
    }
    
    function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'none')
    e.style.display = 'block';
    else
    e.style.display = 'none';
    }
    //all this needs to be in <script> tags at the top
    function setResource (val) {
       var resource = document.getElementById("resource"); //get the input field
       val *= 400; //multiply the value by 400;
    
       resource.value = "" +val; //set the value
      
    }
    
    function setResource (val) {
       var resource = document.getElementById("resource"); //get the input field
       val *= 400; //multiply the value by 400;
    
       resource.value = "" +val; //set the value
     
    }
    
    function setTotal(val) {
      var resourceVal = document.getElementById("resource").value; //get the value
      var total = document.getElemetnById("total");
    
      val = val.replace(//, ''); //remove the "pound" sign
      val = parsefloat(val) //convert to a floating point number (with decimals)
                                 //if its only integers, you can use "parseint(val)"
    
      val += resourceVal; //add the values
    
      total.value = '' +val;  
    } 
    //-->
    
    </script>
    </head>
    <body onLoad="resettoggle()">
    
    <tr>
    					<td><div class="fieldlabel">Change Title:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(1requestfctitle)" rows="2" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Requested By:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(2requestfcrequestor)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">HOTB:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(3requestfcHOTB)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Change Description:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(4requestfcdesc)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>				
    					<td><div class="fieldlabel">Required By Date:</div></td>
    					<td>
    					<a href="#" onClick="cal1.select(document.forms[0]['userstringvalue(5requestfcrequired)'],'anchor1','dd/MM/yy'); return false;" NAME="anchor1" ID="anchor1">
    					<img src="/assystEnterprise/shared/images/calendar.gif" border="0" hspace="0" vspace="0">
    					</a>
    					</td>
    					<td><textarea name="userstringvalue(5requestfcrequired)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>              
    				<tr>
    					<td><div class="fieldlabel">Total Man Days:</div></td>
    					<td></td>
    					<td> <textarea name="manDays" rows="1" class="datafield" onblur="setResource(this.value)"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
                    <tr>
                   	 <td>
                    	<input type="text" id="Resource" size="16">
                    	<br>
                    </td>
                    </tr>
                    
    				<tr>
    					<td><div class="fieldlabel">CapEx Cost ():</div></td>
    					<td></td>
    					<td><textarea name="CapEx" rows="1" class="datafield" onblur="setTotal(this.value)"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				
                   	 <td>
                     <p>total cost
                    	<input type="text" name="Total" size="16">
                    	<br>
                    </td>
                    </tr>
                     
                    
                    <a href="#" onclick="toggle_visibility('extraFields');">Click here if the Total Cost of Change is less than &pound;5k</a><br />
    				
                    
    				
                    <div id="extraFields">
    				<tr>
    					<td colspan="5"><h3>If the Total Cost of Change is less than 5k, please complete the following fields...</h3></td>
    					<td></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Reasons for Change:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(8requestfcreasons)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Known RIDAs:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(9requestfcrida)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Resource Requirements:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(10requestfcresource)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Additional Info:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(11requestfcaddinfo)" rows="2" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>			
    				
    				<tr><td colspan="5"><hr/></td></tr>
                    
                    		<tr>
    			<td colspan="5">
    				</div>
                    <br />
                    
                    <tr>
    					<td colspan="56"><h3>If the Total Cost of Change is more than 5k, please attach a Project Brief to this request.<br><a title="Project Brief" href="file:///S:/!TBPM%20Templates/1.%20Selecting%20Business%20Change/Project%20Brief%20Template%20v1.0.doc" target="_blank">Click here</a> to view the Project Brief template from the <a title="Processes and Controls" href="http://nrfour/files/Intranet%20Spec%20V3.htm" target="_blank">Processes & Controls area</a> of the intranet.</h3></td>
    					<td></td>
    				</tr>
                    <br />
    				<input type="submit" name="LogEvent" value="Submit" onclick="setDispatchTarget('logEvent');" class="button">
    
    </body>
    </html>

  6. #6
    Join Date
    Oct 2007
    Posts
    21
    Just noticed a spelling error: var total = document.getElemetnById("total");

    Still doesn't work even though I have corrected

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script></script>
    
    
    
    <script type="text/javascript">
    <!--
    function resettoggle() {
    var e = document.getElementById('extraFields');
    e.style.display = 'none';
    }
    
    function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'none')
    e.style.display = 'block';
    else
    e.style.display = 'none';
    }
    
    
    function setResource (val) {
       var resource = document.getElementById("resource"); //get the input field
       val *= 400; //multiply the value by 400;
    
       resource.value = "" +val; //set the value
     
    }
    
    function setTotal(val) {
      var resourceVal = document.getElementById("resource").value; //get the value
      var total = document.getElementById("total").value;
    
      val = val.replace(//, ''); //remove the "pound" sign
      val = parseint(val) //convert to a floating point number (with decimals)
                                 //if its only integers, you can use "parseint(val)"
    
      val += resourceVal; //add the values
    
      total.value = '' +val;  
    } 
    //-->
    
    </script>
    </head>
    <body onLoad="resettoggle()">
    
    <tr>
    					<td><div class="fieldlabel">Change Title:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(1requestfctitle)" rows="2" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Requested By:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(2requestfcrequestor)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">HOTB:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(3requestfcHOTB)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Change Description:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(4requestfcdesc)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>				
    					<td><div class="fieldlabel">Required By Date:</div></td>
    					<td>
    					<a href="#" onClick="cal1.select(document.forms[0]['userstringvalue(5requestfcrequired)'],'anchor1','dd/MM/yy'); return false;" NAME="anchor1" ID="anchor1">
    					<img src="/assystEnterprise/shared/images/calendar.gif" border="0" hspace="0" vspace="0">
    					</a>
    					</td>
    					<td><textarea name="userstringvalue(5requestfcrequired)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>              
    				<tr>
    					<td><div class="fieldlabel">Total Man Days:</div></td>
    					<td></td>
    					<td> <textarea name="manDays" rows="1" class="datafield" onblur="setResource(this.value)"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
                    <tr>
                   	 <td>
                    	<input type="text" id="Resource" size="16">
                    	<br>
                    </td>
                    </tr>
                    
    				<tr>
    					<td><div class="fieldlabel">CapEx Cost ():</div></td>
    					<td></td>
    					<td><textarea name="CapEx" rows="1" class="datafield" onblur="setTotal(this.value)"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				
                   	 <td>
                     <p>total cost
                    	<input type="text" id="Total" size="16">
                    	<br>
                    </td>
                    </tr>
                     
                    
                    <a href="#" onclick="toggle_visibility('extraFields');">Click here if the Total Cost of Change is less than &pound;5k</a><br />
    				
                    
    				
                    <div id="extraFields">
    				<tr>
    					<td colspan="5"><h3>If the Total Cost of Change is less than 5k, please complete the following fields...</h3></td>
    					<td></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Reasons for Change:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(8requestfcreasons)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Known RIDAs:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(9requestfcrida)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Resource Requirements:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(10requestfcresource)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Additional Info:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(11requestfcaddinfo)" rows="2" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>			
    				
    				<tr><td colspan="5"><hr/></td></tr>
                    
                    		<tr>
    			<td colspan="5">
    				</div>
                    <br />
                    
                    <tr>
    					<td colspan="56"><h3>If the Total Cost of Change is more than 5k, please attach a Project Brief to this request.<br><a title="Project Brief" href="file:///S:/!TBPM%20Templates/1.%20Selecting%20Business%20Change/Project%20Brief%20Template%20v1.0.doc" target="_blank">Click here</a> to view the Project Brief template from the <a title="Processes and Controls" href="http://nrfour/files/Intranet%20Spec%20V3.htm" target="_blank">Processes & Controls area</a> of the intranet.</h3></td>
    					<td></td>
    				</tr>
                    <br />
    				<input type="submit" name="LogEvent" value="Submit" onclick="setDispatchTarget('logEvent');" class="button">
    
    </body>
    </html>

  7. #7
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Most of the errors had to do with letter casing being wrong. Make both input id's all lowercase. Also for some reason the replace wasn't replacing the $ sign, so I put in a splice instead.
    Code:
    function setResource (val) {
       var resource = document.getElementById("resource"); //get the input field
       val *= 400; //multiply the value by 400;
    
       resource.value = "$" +val; //set the value
     
    }
    
    function setTotal(val) {
      var resourceVal = document.getElementById("resource").value; //get the value
      var total = document.getElementById("total");
    
      var rVal = resourceVal.slice(1); //remove the "pound" sign
      alert(rVal)
      resourceVal = parseInt(rVal) //convert to a floating point number (with decimals)
                                 //if its only integers, you can use "parseint(val)"
    
      val += rVal; //add the values
      alert (val)
      total.value = '$' +val;  
    }

  8. #8
    Join Date
    Oct 2007
    Posts
    21
    Yes, this works great but I'm still encountering the same problem with what I started with, it seems to be adding the numbers as strings and not adding them together as numbers? sorry if I'm being a pain, I do really appreciate your help!

  9. #9
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    I tested the functions and they both worked as numbers for me.

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259
    Try using 'parseInt(strval)' or 'parseFloat(strval)' in your calculations involving strings.

  11. #11
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Oh I see the problem. For some reasone the code I copied and pasted wasn't the same as the one I had. Fixed:
    [code]
    function setTotal(val) {
    var resourceVal = document.getElementById("resource").value; //get the value
    var total = document.getElementById("total");

    var rVal = resourceVal.slice(1); //remove the "pound" sign
    alert(rVal)
    rVal = parseInt(rVal) //convert to a floating point number (with decimals)
    //if its only integers, you can use "parseint(val)"

    val += rVal; //add the values
    alert (val)
    total.value = '$' +val;
    }

  12. #12
    Join Date
    Oct 2007
    Posts
    21
    I can't work this out at all! Instead of 800 + 2 = 802 I get 2800

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script></script>
    
    
    
    <script type="text/javascript">
    <!--
    function resettoggle() {
    var e = document.getElementById('extraFields');
    e.style.display = 'none';
    }
    
    function toggle_visibility(id) {
    var e = document.getElementById(id);
    if(e.style.display == 'none')
    e.style.display = 'block';
    else
    e.style.display = 'none';
    }
    
    
    function setResource (val) {
       var resource = document.getElementById("resource"); //get the input field
       val *= 400; //multiply the value by 400;
    
       resource.value = "" +val; //set the value
     
    }
    
    function setTotal(val) {
      var resourceVal = document.getElementById("resource").value; //get the value
      var total = document.getElementById("total");
    
      var rVal = resourceVal.slice(1); //remove the "pound" sign
      alert(rVal)
      resourceVal = parseInt(rVal) //convert to a floating point number (with decimals)
                                 //if its only integers, you can use "parseint(val)"
    
      val += rVal; //add the values
      alert (val)
      total.value = '' +val;  
    } 
    //-->
    
    </script>
    </head>
    <body onLoad="resettoggle()">
    
    <tr>
    					<td><div class="fieldlabel">Change Title:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(1requestfctitle)" rows="2" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Requested By:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(2requestfcrequestor)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">HOTB:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(3requestfcHOTB)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Change Description:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(4requestfcdesc)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>				
    					<td><div class="fieldlabel">Required By Date:</div></td>
    					<td>
    					<a href="#" onClick="cal1.select(document.forms[0]['userstringvalue(5requestfcrequired)'],'anchor1','dd/MM/yy'); return false;" NAME="anchor1" ID="anchor1">
    					<img src="/assystEnterprise/shared/images/calendar.gif" border="0" hspace="0" vspace="0">
    					</a>
    					</td>
    					<td><textarea name="userstringvalue(5requestfcrequired)" rows="1" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>              
    				<tr>
    					<td><div class="fieldlabel">Total Man Days:</div></td>
    					<td></td>
    					<td> <textarea name="manDays" rows="1" class="datafield" onblur="setResource(this.value)"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
                    <tr>
                   	 <td>
                    	<input type="text" id="resource" size="16">
                    	<br>
                    </td>
                    </tr>
                    
    				<tr>
    					<td><div class="fieldlabel">CapEx Cost ():</div></td>
    					<td></td>
    					<td><textarea name="CapEx" rows="1" class="datafield" onblur="setTotal(this.value)"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				
                   	 <td>
                     <p>total cost
                    	<input type="text" id="total" size="16">
                    	<br>
                    </td>
                    </tr>
                     
                    
                    <a href="#" onclick="toggle_visibility('extraFields');">Click here if the Total Cost of Change is less than &pound;5k</a><br />
    				
                    
    				
                    <div id="extraFields">
    				<tr>
    					<td colspan="5"><h3>If the Total Cost of Change is less than 5k, please complete the following fields...</h3></td>
    					<td></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Reasons for Change:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(8requestfcreasons)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Known RIDAs:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(9requestfcrida)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Resource Requirements:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(10requestfcresource)" rows="3" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>
    				<tr>
    					<td><div class="fieldlabel">Additional Info:</div></td>
    					<td></td>
    					<td><textarea name="userstringvalue(11requestfcaddinfo)" rows="2" class="datafield"></textarea></td>
    					<td></td>
    					<td><div class="errormsg"></div></td>
    				</tr>			
    				
    				<tr><td colspan="5"><hr/></td></tr>
                    
                    		<tr>
    			<td colspan="5">
    				</div>
                    <br />
                    
                    <tr>
    					<td colspan="56"><h3>If the Total Cost of Change is more than 5k, please attach a Project Brief to this request.<br><a title="Project Brief" href="file:///S:/!TBPM%20Templates/1.%20Selecting%20Business%20Change/Project%20Brief%20Template%20v1.0.doc" target="_blank">Click here</a> to view the Project Brief template from the <a title="Processes and Controls" href="http://nrfour/files/Intranet%20Spec%20V3.htm" target="_blank">Processes & Controls area</a> of the intranet.</h3></td>
    					<td></td>
    				</tr>
                    <br />
    				<input type="submit" name="LogEvent" value="Submit" onclick="setDispatchTarget('logEvent');" class="button">
    
    </body>
    </html>

  13. #13
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259
    Are you sure that:
    PHP Code:
      var total document.getElementById("total"); 
    should not be:
    PHP Code:
      var total document.getElementById("total").value
    Also, have you been checking the error console?
    What error messages (if any)?

  14. #14
    Join Date
    Oct 2007
    Posts
    21
    I'm not getting any error now it just doesn't calculate:

    Instead of 800 + 2 = 802 I get 2800

  15. #15
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    You didn't use the updated code I posted. In the code you posted the parseInt(rVal) is being stored as resourceVal, which is incorrect. It should be rVal=parseInt(rVal)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles