473,320 Members | 1,862 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,320 software developers and data experts.

Adding checkbox values together - works but got small problem

1
This is my first time posting to the forum and welcome assistance to fix this javascript:

[HTML]<script language="JavaScript">
<!--
function calculate(what) {
what.answer.value =0;
for (var i=1,answer=0;i<6;i++)
answer += (what.elements['CourseOrder' + i].checked)&(what.elements['CourseOrder' + i].value-0);
what.answer.value = answer;
}
//-->
</script>

<form>
<INPUT type="checkbox" NAME="CourseOrder1" VALUE="1" onclick="calculate(this.form)"><br>
<INPUT type="checkbox" NAME="CourseOrder2" VALUE="1" onclick="calculate(this.form)"><br>
<INPUT type="checkbox" NAME="CourseOrder3" VALUE="1" onclick="calculate(this.form)"><br>
<INPUT type="checkbox" NAME="CourseOrder4" VALUE="1" onclick="calculate(this.form)"><br>
<INPUT type="checkbox" NAME="CourseOrder5" VALUE="5" onclick="calculate(this.form)"><br>

<input type="text" name="answer">

</form>[/HTML]

I want to change the values, but when the values are changed it does not work or even provide the correct value. It is treating is checkbox as 1 and doesn't see the value of the checkbox?

Any help welcome,

Kevin
Apr 4 '08 #1
2 1453
hsriat
1,654 Expert 1GB
Replace what with document.forms[0] everywhere in the function.
And don't pass any argument to the function calculate()


Regards
Apr 4 '08 #2
acoder
16,027 Expert Mod 8TB
The problem is caused by the shorthand notation "&". Try this instead:
Expand|Select|Wrap|Line Numbers
  1. if (what.elements['CourseOrder' + i].checked) 
  2.   answer += parseInt(what.elements['CourseOrder' + i].value);
Apr 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Homa | last post by:
Hi, I have a Datagrid that uses as a shopping cart and have a checkbox template column in it. I can't add both OnCheckedChanged Event and onclick client script for it. I want to do two...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
8
by: Alan Silver | last post by:
Hello, I have a repeater that has code like this... <ItemTemplate> <asp:CheckBox ID="chkDelete" Text="" RunAt="server"/> .... other stuff goes here </ItemTemplate> There is a button below...
1
by: Kevin R | last post by:
This is one of the weirdest problems I have ever run into. I have had to trim down a bunch of code to give a sample that is more easily readable by those who will view this. Here is the problem:...
0
by: deathtospam | last post by:
A few weeks ago, I created a Classic ASP page that connects to a machine with SQL Server installed on it, prompts the user to select a database on that server, then lists all of user-created stored...
4
by: Matt | last post by:
I am no JavaScript guru so please bear with me and be as detailed as possible with your response. I thank you in advance. I have an ASP page that contains form elements. I also have an inline...
11
by: =?Utf-8?B?UGFyYWcgR2Fpa3dhZA==?= | last post by:
Hi All, I have a large recordset to be displayed on a ASP 3.0 page. I am using recordset paging for this. For the next and previous link i am passing href as <a href=<Page URl>?page=<%=...
3
by: Mahathi | last post by:
Hi I have a small problem in maintaining the state of a check box. Please do me a favour by telling me the procedure how to do that. My requirement is that "I have to map some roles with...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.