470,618 Members | 1,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,618 developers. It's quick & easy.

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 1397
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Stone Chen | last post: by
1 post views Thread by The Eclectic Electric | last post: by
4 posts views Thread by ballygowanboy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.