473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Adding to fields with onChange

HI!

Have two from text fields named "odds" and "stake" where I enter numeric
values.
I want to have a text label named "returns" that is automatically updated
whenever any of these changes.

Here's my first try (I have cut this out from the form, named form_bet):

<input name="odds" type="text" id="odds" onChange="updateReturns();"
value="0">
<input name="stake" type="text" id="stake" onChange="updateReturns();"
value="0">
<label id="returns">0</label>

This is my function (inside a script tag in HEAD)

function updateReturns(){
var returned;
returned = document.form_bet.odds.value * document.form_bet.stake.value;
document.form_bet.returns.value = returned;
}

Doesn't seem to work. Can anyone help?

Quinn
Jul 23 '05 #1
2 1526
In article <Vy******************@news2.e.nsc.no>, Quinn <st***@mk.no>
wrote:
HI!

Have two from text fields named "odds" and "stake" where I enter numeric
values.
I want to have a text label named "returns" that is automatically updated
whenever any of these changes.

Here's my first try (I have cut this out from the form, named form_bet):

<input name="odds" type="text" id="odds" onChange="updateReturns();"
value="0">
<input name="stake" type="text" id="stake" onChange="updateReturns();"
value="0">
<label id="returns">0</label>

This is my function (inside a script tag in HEAD)

function updateReturns(){
var returned;
returned = document.form_bet.odds.value * document.form_bet.stake.value;
document.form_bet.returns.value = returned;
}

Doesn't seem to work. Can anyone help?

Quinn

Try the following

<html >
<head>
<title>Untitled</title>
<script type="text/javascript">
function updatereturns(form) {
form.returns.value = form.odds.value * form.stake.value;
}
</script>
</head>
<body>
<form>
<input type="text" name="odds" onchange="updatereturns(this.form);"
value="0">
<input type="text" name="stake" onchange="updatereturns(this.form);"
value="0">
<input type="text" name="returns" readonly="readonly" value="0">
</form>
</body>
</html>

--
Dennis Marks
http://www.dcs-chico.com/~denmarks/
To reply change none to dcsi.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #2
The code below should help you. Some remarks:
- innerText is not for e.g. NS4, but you already use <label>
- why not onKeyPress in stead of onChange? Updates your <label>
immediately
- you could add an onKeyPress event so that user cannot type
non-numeric characters in the fields and thus avoiding non-numeric
results inside your <label>. See here how this could be done:
http://groups.google.be/groups?th=25e6623a95461f74

hope this helps you further
Bart
<html>
<head>
<script language="javascript">
function updateReturns(){
var returned;
returned = parseFloat(document.form_bet.odds.value)
* parseFloat(document.form_bet.stake.value);
returns.innerText = returned
}
</script>
</head>
<body>
<form name="form_bet">
<input name="odds" type="text" id="odds" onChange="updateReturns();"
value="0">
<input name="stake" type="text" id="stake" onChange="updateReturns();"
value="0">
<label id="returns">0</label>
</form>
</body>
</html>
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: please-answer-here | last post by:
Scenario: I'm having an asp page with a form with an action pointing to itself. The reason for that, it that the asp page depending on changes in any of two select fields with data from 2...
4
by: Stone Chen | last post by:
Hello, I have form that uses javascript createElement to add additional input fields to it. However, my validating script will not process new input fields because it can only find the named...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
4
by: ballygowanboy | last post by:
i've put this code together. there's a variable "s" giving me some grief at the mo, i'm actualy supprised it half works. it's a simple shopping cart, you pick the quantitly of items, and it...
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
7
by: TriAdmin | last post by:
I am working with a system that allow me to add custom fields but I can not add OnChange() language to the custom fields. So I want to have a function in the header that recognizes when fieldx is...
3
by: pzh20 | last post by:
I have an unbound form/subform where I populate a combo box on the main form, and using the onchange event, display fields from a table in a datasheet subform. I want to add a new record via the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.