473,406 Members | 2,549 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 the contents of a form with 'OnChange'?

I'm having trouble creating a very simple running total of a
few drop down list. I have three seperate dropdown lists, I
want the user to simply select some numbers and have the
total of the selected numbers displayed.

I'm not much of a programmer and this is really just a
experiment. Can anyone help me out here. I know the code
below doesnt work (no form tags etx0 but I thought the logic
was fine?

Example of what I would like to happen

User selects 2,5,6 and a total of 13 is displayed somewhere
on the page. This total changes when the user picks
different numbers.

Can anyone help me out here?

------------------------------------------------------------
--------
Code

</head>
<script language="javascript">
function workit()
{
var itemA=document.select.value
var itemB=document.select2.value
var itemC=document.select3.value
var total = itemA+itemB+itemC;
}
</script>
<body>
<p>
<select name="select" onChange="workit()">
<option value="1" selected>One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select name="select2" onChange="workit()">
<option value="4" selected>Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
</select>
<select name="select3" onChange="workit()">
<option value="7" selected>Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
</select>
</p>
<p><span id=mytotal></span></p>
</body>
</html>
Jul 23 '05 #1
5 3578

I've been picking up javascript these past six months - I think my
single line below should do what you want to do, though you might find
one or two others who might correct my methods...

</head>
<script language="javascript">
function workit()
{
var itemA=document.select.value
var itemB=document.select2.value
var itemC=document.select3.value
var total = itemA+itemB+itemC; document.getElementById('mytotal').innerHTML=total ; }
</script>
<body>
<p>
<select name="select" onChange="workit()">
<option value="1" selected>One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select name="select2" onChange="workit()">
<option value="4" selected>Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
</select>
<select name="select3" onChange="workit()">
<option value="7" selected>Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
</select>
</p>
<p><span id=mytotal></span></p>
</body>
</html>


Hope I've helped,
randelld
Jul 23 '05 #2
Reply Via Newsgroup wrote:
I've been picking up javascript these past six months - I think my single line below should do what you want to do, though you might find one or two others who might correct my methods...


thanks m8, I will give it a go.

Jul 23 '05 #3

"Cheddar" <my*************@dsl.pipex.com> a écrit dans le message de
news:c5************@ID-179732.news.uni-berlin.de...
I'm having trouble creating a very simple running total of a
few drop down list. I have three seperate dropdown lists, I
want the user to simply select some numbers and have the
total of the selected numbers displayed.

I'm not much of a programmer and this is really just a
experiment. Can anyone help me out here. I know the code
below doesnt work (no form tags etx0 but I thought the logic
was fine?

Example of what I would like to happen

User selects 2,5,6 and a total of 13 is displayed somewhere
on the page. This total changes when the user picks
different numbers.

Can anyone help me out here?

------------------------------------------------------------
--------
Code

</head>
<script language="javascript">
function workit()
{
var itemA=document.select.value
var itemB=document.select2.value
var itemC=document.select3.value
var total = itemA+itemB+itemC;
Try this instead:
function workit()
{
var itemA = parseInt(select.options[select.selectedIndex].value);
var itemB = parseInt(select2.options[select2.selectedIndex].value);
var itemC = parseInt(select3.options[select3.selectedIndex].value);

var total = itemA + itemB + itemC;
mytotal.innerHTML = total;
}
}
</script>
<body>
<p>
<select name="select" onChange="workit()">
<option value="1" selected>One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select name="select2" onChange="workit()">
<option value="4" selected>Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
</select>
<select name="select3" onChange="workit()">
<option value="7" selected>Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
</select>
</p>
<p><span id=mytotal></span></p>
</body>
</html>

Jul 23 '05 #4
Morris wrote:
Try this instead:
function workit()
{
var itemA = parseInt(select.options[select.selectedIndex].value); var itemB =
parseInt(select2.options[select2.selectedIndex].value); var
itemC = parseInt(select3.options[select3.selectedIndex].value);

var total = itemA + itemB + itemC;
mytotal.innerHTML = total;
}


Good stuff, thanks a lot.


Jul 23 '05 #5
JRS: In article <0k********************@wagner.videotron.net>, seen in
news:comp.lang.javascript, Morris <mo********@eudoramail.com> posted at
Wed, 14 Apr 2004 17:47:39 :

Try this instead:
function workit()
{
var itemA = parseInt(select.options[select.selectedIndex].value);


But only after considering whether the string .value might have a
leading zero, and what (a) might happen, (b) is needed, in that case.

Read the FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6

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

Similar topics

2
by: Quinn | last post by:
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. ...
3
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a...
4
by: engwar | last post by:
I'd like to know if it's possible to change the contents of a div tag based on something the user is doing. For example. If there is a text box on a page and the user types his or her name can I...
5
by: Neo Geshel | last post by:
Greetings. I am in a very big pickle. I am trying to add page content - as well as a submit button - programatically to a web form that is supposed to submit to DB and then refresh. That...
2
by: darrel | last post by:
I am using a javascript WYSIWYG text editor for our CMS. To grab the proper content we need to add a javacsript call to the form: Form1.Attributes.Add("onSubmit", "myOnSubmitEventHandler();") ...
1
by: mayan67 | last post by:
I am trying to add sound, video and a rolling credit class applet. I have put in the information and nothing works. I am not sure what I have done wrong This is just a simple web page using some...
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...
6
by: eureka | last post by:
Hi friends, I am developing a web application using Jsp and JS. I have a main Jsp page(Jsp1).Inside it I have an iframe having an Html- table which is created dynamically and contains all...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.