By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,462 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,462 IT Pros & Developers. It's quick & easy.

Adding to fields with onChange

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.