Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to implement an automatic JS-multiplication in a combo box 1

Status
Not open for further replies.

leifoet

Technical User
Jan 31, 2016
203
BE
In a MS Access table i will update the following two fields :
=> an input field called NUMBER => to fill in a selection (=number) from a list box (see below)
=> a field called AMOUNT in which automatically (the entered NUMBER * x) is (should be) calculated => the result should be displayed in this AMOUNT-field

My tryouts did not work.
Is it possible to use Javascript in my code ?
Thanks for tips - Leifoet


My code
.....
<tr>
<td><b>Number_2016<b></b></td>
<td width="353">

<SELECT name="Number_2016" style="font-size:13px; color:#0000FF; text-align:right; background-color:#E0FFFF;">
<OPTION SELECTED value="<%=rsTEST("team1")%>" style="font-family:arial,sans-serif;font-size:12px;font-weight:bold;
font-size:12px; color:#0000FF; text-align:left; background-color:#FFFAFA;"><%=rsTEST("Number_2016")%></option>
<option value=0 style="font-family:arial,sans-serif;font-size:12px; color:#FF1616;">0</option>
<option value=1 style="font-family:arial,sans-serif;font-size:12px; color:#0000FF;">1</option>
<option value=2 style="font-family:arial,sans-serif;font-size:12px; color:#0000FF;">2</option>
<option value=3 style="font-family:arial,sans-serif;font-size:12px; color:#0000FF;">3</option>
.... up to 100

[indent</select><br>
</td></tr>

<tr>
<td height="26"><b>Amount_2016</b></td>
<td height="26" width="353">
<INPUT TYPE="number" size="12" value="<%=rsFOCOTEST("Amount_2016")%>" NAME="Amount" onFocus="blur()" style="font-weight:bold; font-family:arial,sans-serif;
font-size:12px; color:#0000FF; text-align:right;"><font size="1" color="black">&nbsp;&nbsp;&nbsp;(niet wijzigen)</td>

</tr>

 
You could use the onChange event of the select element to pass the selected option value to your calculate function.


onChange="calculateFunction(this.selectedIndex.value)"

Or if you want the values to update as the document loads you can use the body onLoad event.

calculateFunction(document.getElementById("SelectID").value)

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
As I understand it, I write in the body (input Number field): onChange="calculateFunction(this.selectedIndex.value)"
Sorry for my two further problems (I am only a hobby 'programmer' for my sports club)
=> what is also the Java-code to be written in the head ?
=> can I become the output of this calculation directly in the Amount-(input)field of the form (as mentioned in my initial question above)?

Thanks for (a bit more) details - Leifoet

 
Java-code

javascript (little 'j') code, as Java (capital 'J') code is a whole different language with syntax similarities



The calculateFunction script which may or may not be defined in the <head> section of the document is the multiplication code, which is at it's simplest;

JavaScript:
<script type="text/javascript">
function calculateFunction(p_iMult) {
document.getElementById('amount').value = document.getElementById('number').value * p_iMult;
}
</script>
You do of course have to add the necessary ID attributes to you form elements, and call the function from the onBlur event of the 'number' input element so the calculateFunction runs when the element loses focus.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Preliminary final question in my javascript matter (amount = number * p_iMult) => where comes the exact value (=75) for this p_iMult in the script ?

<script type="text/javascript">
function calculateFunction(p_iMult) {
document.getElementById('amount').value = document.getElementById('number').value * p_iMult;
}
</script>

Thanks - Leifoet
 
where comes the exact value (=75) for this p_iMult in the script


function calculateFunction(p_iMult)

from here ..................^^^^^^

you pass it as a parameter when you call the function, as you can see in the earlier examples:

onChange="calculateFunction(this.selectedIndex.value)"

..............................^^^^^^^^^^^^^^^^^^^^^..... becomes p_iMult in the function


the p_i... is just my variable naming method

"p_" indicates it is a passed parameter

"i" indicates it is a integer

so "p_i" means it a an integer value passed as a parameter to the function.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top