?
Solved

How to create function to add two text boxes together

Posted on 2009-04-22
8
Medium Priority
?
489 Views
Last Modified: 2012-05-06
Hi experts, please can someone help me with a problem im encountering. Basically i have a page which shows various figures for different currencies, i have just added in three extra text boxes (txtprefamount, txtaddamount, txttotal), and what i want to do is to create a function to add together the values from txtprefamount and txtaddamount and put the total in txttotal, the problem is that im pulling the currency symbols from sql and thus it create's the text boxes indvidually and i need to calculate the two figures individually for each currency. I know this should be simple enough but im a bit of a novice when it comes to functions. Any help is appreciated. Thanks
<TABLE border=0 cellpadding=0 cellspacing="4" width="40%">
	<tr bgcolor="#CC6600">
		<td colspan=3 width="100%"><font size="2"><img src="images/blank.gif" width="8" height="1"></font></td>
	</tr>
<p>&nbsp;</p>
      <tr> 
               
	
		<TD bgColor=#ffffff width="50%"><font color="#00599d" face="Verdana, Arial, Helvetica, sans-serif" size="2">Currency</font></TD>
        <TD bgColor=#ffffff width="50%"><font color="#00599d" face="Verdana, Arial, Helvetica, sans-serif" size="2">Recommended Amount</font></TD>
        <TD bgColor=#ffffff width="50%"><font color="#00599d" face="Verdana, Arial, Helvetica, sans-serif" size="2">Prepared Amount</font></TD>
        <TD bgColor=#ffffff width="50%"><font color="#00599d" face="Verdana, Arial, Helvetica, sans-serif" size="2">Additional Amount</font></TD>
        <TD bgColor=#ffffff width="50%"><font color="#00599d" face="Verdana, Arial, Helvetica, sans-serif" size="2">Total Sell Off</font></TD>
	
      </tr>
      <tr bgcolor="#CC6600">
		<td colspan=3 width="100%"><font size="2"><img src="images/blank.gif" width="8" height="1"></font></td>
	</tr>
	    <tr>
	    
	    <TD bgColor=#ffffff width="50%"><font color="#00599d" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Main Currencies</b></font></TD>
	    </tr>
      <%
      rsType.movefirst
      do until rsType.eof
		iDefaultCashCentre  = rsType("SellOffCashCentre")
      %>
      <tr>
 
        <td width="50%" bgcolor="#FFFFFF">
			<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><%=rsType("currencysymbol")%>
				<input type=hidden name="txtsym<%=CurrInt%>" maxlength="10%" size="22%" value="<%=rsType("currencysymbol")%>">
				<input type=hidden name="txtCurrency<%=CurrInt%>" maxlength="10%" size="22%" value="<%=rsType("SellOffCurrency")%>">
			</font>
		</td>
        <td width="50%" bgcolor="#FFFFFF"><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399">
        <input name=txtCash<%=CurrInt%> maxlength="9"
		<%if rsType("SellOffStatus") = "Sent" then%> 
			value="0" 
        <%else%>
			value="<%=rsType("SellOffacAmount")%>" 
        <%end if%> 
		size="22%"></font>
		<input type=hidden name=txtStatus<%=CurrInt%> value="<%=rsType("SellOffStatus")%>">
 
		</td>
			<input type=hidden name="lstCashCentre<%=CurrInt%>" value=<%=arrCashCentre(0,0)%>>
		
		<input type=hidden name="lstMinSellOff<%=CurrInt%>" value=<%=rsType("minSellOff")%>>
	    <td>
		<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><input type=text value=0 name=txtprefamount onChange="javascript:add()"/></font>
      </td>
        <td>
		<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><input type=text value=0 name=txtaddamount onChange="javascript:add()"/></font>
      </td>
      <td>
		<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><input type=text value=0 name=txttotal onChange="javascript:add()"/></font>
      </td>
      </tr>
<%
CurrInt=CurrInt+1
rsType.MoveNext
loop
%>
 
<tr bgcolor="#CC6600">
		<td colspan=3 width="100%"><font size="2"><img src="images/blank.gif" width="8" height="1"></font></td>
	</tr>
	    <tr>
	    
	    <TD bgColor=#ffffff width="50%"><font color="#00599d" face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Exotic Currencies</b></font></TD>
	    </tr>
      <%
      rsOther.movefirst
      do until rsOther.eof
		iDefaultCashCentre  = rsOther("SellOffCashCentre")
      %>
      <tr>
 
        <td width="50%" bgcolor="#FFFFFF">
			<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><%=rsOther("currencysymbol")%>
				<input type=hidden name="txtsym<%=CurrInt%>" maxlength="10%" size="22%" value="<%=rsOther("currencysymbol")%>">
				<input type=hidden name="txtCurrency<%=CurrInt%>" maxlength="10%" size="22%" value="<%=rsOther("SellOffCurrency")%>">
			</font>
		</td>
        <td width="50%" bgcolor="#FFFFFF"><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399">
        <input name=txtCash<%=CurrInt%> maxlength="9"
		<%if rsOther("SellOffStatus") = "Sent" then%> 
			value="0" 
        <%else%>
			value="<%=rsOther("SellOffacAmount")%>" 
        <%end if%> 
		size="22%"></font>
		<input type=hidden name=txtStatus<%=CurrInt%> value="<%=rsOther("SellOffStatus")%>">
 
		</td>
			<input type=hidden name="lstCashCentre<%=CurrInt%>" value=<%=arrCashCentre(0,0)%>>
		
		<input type=hidden name="lstMinSellOff<%=CurrInt%>" value=<%=rsOther("minSellOff")%>>
	   <td>
		<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><input type=text value=0 name=txtprefamount onChange="javascript:add()"/></font>
      </td>
        <td>
		<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><input type=text value=0 name=txtaddamount onChange="javascript:add()"/></font>
      </td>
      <td>
		<font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#003399"><input type=text value=0 name=txttotal onChange="javascript:add()"/></font>
      </td>
      </tr>
      </tr>
<%
CurrInt=CurrInt+1
rsOther.MoveNext
loop
%>

Open in new window

0
Comment
Question by:cmsRuncorn
  • 4
  • 3
8 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24203907
Currently, you have/use:

<input type=text value=0 name=txtprefamount onChange="javascript:add()"/>
<input type=text value=0 name=txtaddamount onChange="javascript:add()"/>
<input type=text value=0 name=txttotal onChange="javascript:add()"/>

You probably want to make txttotal as readonly, without the onChange (the user shouldn't be specifying the total).

It would also be easier/better if you used the id attribute, instead of the name attribute for these fields, since the id attribute is supposed to be unique on the page, whereas elements that are to be grouped together can, and should share the same name.

How will the numeric values look?

What unit separators can & will exist?

Where will the currency characters be placed?

Can currency characters be empty, or must they be present?
0
 

Author Comment

by:cmsRuncorn
ID: 24204360
Thanks for your reply, and i shall change the txttotal field to read only as that makes more sense.
As for the id attribute, i can't say i've used it before so i would be unaware of the syntax but again that does sound better than using the name tag.

The numeric values will have no seperators and will be to 1 decimal place so no format change is required i think. The  currency characters are actually currency names which show up as shortnames for the currencies (i.e. EUR for Euro and GBP for Pound Sterling etc...). The currency names must be there as this page will be an ordering page and they are the unique identifier.

Please find attached a cropped example of the page, this shows the main currencies, the exotic currencies are just below (but wouldn't fit on!) , the page loops through the sql string and pulls back all requested currencies. I hope this clarifies things a bit more and thanks for the help


example.jpg
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 800 total points
ID: 24204581
To use the id attribute, instead of name, they would look like:

<input type='text' value='0' id='txtprefamount' onChange='javascript:add()'/>
<input type='text' value='0' id='txtaddamount'  onChange='javascript:add()'/>
<input type='text' value='0' id='txttotal'      readonly/>

Something simple like this may work for you...
function field( id ) {
  var ele = document.getElementById( id )
  if ( !ele ) {
    alert( 'Specified element not found. id="' + id + '"' )
  }
  return ele
}
 
function add() {
  var pre   = field( 'txtprefamount' )
  var add   = field( 'txtaddamount'  )
  var total = field( 'txttotal' )
  if ( pre && add && total ) {
    total.value = ( parseFloat( pre.value ) + parseFloat( add.value ) ).toFixed( 2 )
  }
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:cmsRuncorn
ID: 24213015
Hi, thanks for the reply, i have implemented what you suggested and it only works on the first line, the rest of the currencies do no do anything? So if you look at the example the top row calculates correctly but all the rows below do nothing.
0
 
LVL 4

Assisted Solution

by:CoveyCraig
CoveyCraig earned 200 total points
ID: 24236453
Place the functions at the end of your script (before closing %>), then call them from within your loop.  
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24236873
What else needs to be done for you with respect to this question?
0
 

Author Closing Comment

by:cmsRuncorn
ID: 31573224
Thanks a lot for all your help, that's exactly what i needed.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24241053
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question