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

Why does this statement give an error when the I hv one checkbox?

I have a page with checkboxes generated by a script. Users are suppose
to select the checkboxes and click on a button to process their
selection. The codes work fine except when I only have one checkbox on
the page. Then it will not register sReturn. Can anyone help me out,
why this occur when there is only 1 checkbox on the page. Thanks in
advance.
function processOutcome(){
mainDatabase=document.forms[0].AssessDatabase.value
var oCheckboxs=document.forms[0].TeamID
var sReturn="";
for (var i=0;i<oCheckboxs.length;i++){
if (oCheckboxs[i].checked)
if (sReturn=="")
sReturn=oCheckboxs[i].value;
else
sReturn=sReturn+","+oCheckboxs[i].value;
}
if (sReturn=="") {
alert("Select a team for Outcome Assessment"); }
else {
window.location="/"+mainDatabase+"/outcome?openform&TeamID=" +sReturn}

}

Jul 23 '05 #1
6 1301
effe...@epitome.com.sg wrote:
I have a page with checkboxes generated by a script. Users are suppose to select the checkboxes and click on a button to process their
selection. The codes work fine except when I only have one checkbox on the page. Then it will not register sReturn. Can anyone help me out,
why this occur when there is only 1 checkbox on the page. Thanks in
advance.
function processOutcome(){
mainDatabase=document.forms[0].AssessDatabase.value
var oCheckboxs=document.forms[0].TeamID
var sReturn="";
for (var i=0;i<oCheckboxs.length;i++){
if (oCheckboxs[i].checked)
if (sReturn=="")
sReturn=oCheckboxs[i].value;
else
sReturn=sReturn+","+oCheckboxs[i].value;
}
if (sReturn=="") {
alert("Select a team for Outcome Assessment"); }
else {
window.location="/"+mainDatabase+"/outcome?openform&TeamID=" +sReturn}
}


function processOutcome(){
mainDatabase=document.forms[0].AssessDatabase.value;
var oCheckboxs=document.forms[0].TeamID;
oCheckboxs = ('undefined' != typeof oCheckboxs[0]) ?
oCheckboxs : [oCheckboxs];
var sReturn="";
for (var i=0;i<oCheckboxs.length;i++){
if (oCheckboxs[i].checked)
........

Jul 23 '05 #2
RobB wrote:
var oCheckboxs=document.forms[0].TeamID;
oCheckboxs = ('undefined' != typeof oCheckboxs[0]) ?
oCheckboxs : [oCheckboxs];


var oCheckboxs = [].concat(document.forms[0].TeamID);

ciao, dhgm
Jul 23 '05 #3
ef*****@epitome.com.sg wrote:

In addition to the two other answers, I would replace
[...]
var sReturn="";
for (var i=0;i<oCheckboxs.length;i++){
if (oCheckboxs[i].checked)
if (sReturn=="")
sReturn=oCheckboxs[i].value;
else
sReturn=sReturn+","+oCheckboxs[i].value;
}
[...]
window.location=[...] +sReturn


with

var aReturn = [];
for (var i=0; i<oCheckboxs.length; i++) {
if (oCheckboxs[i].checked) {
aReturn[aReturn.length] = oCheckboxs[i].value;
}
}
[...]
window.location = [...] + aReturn.join(",");

ciao, dhgm
Jul 23 '05 #4
Dietmar Meier wrote:
ef*****@epitome.com.sg wrote:

In addition to the two other answers, I would replace
[...]
var sReturn="";
for (var i=0;i<oCheckboxs.length;i++){
if (oCheckboxs[i].checked)
if (sReturn=="")
sReturn=oCheckboxs[i].value;
else
sReturn=sReturn+","+oCheckboxs[i].value;
}
[...]
window.location=[...] +sReturn


with

var aReturn = [];
for (var i=0; i<oCheckboxs.length; i++) {
if (oCheckboxs[i].checked) {
aReturn[aReturn.length] = oCheckboxs[i].value;
}
}
[...]
window.location = [...] + aReturn.join(",");

ciao, dhgm

In addition to the three other answers... &:~D

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">

function processOutcome(els)
{
var TIDs = els.TeamID, id, vals = [];
if ('undefined' == typeof TIDs[0])
TIDs = [TIDs];
for (var i = 0, l = TIDs.length; i < l; ++i)
{
id = TIDs[i];
if (id.checked)
vals.push(id.value);
}
if (!vals.length)
{
alert('\nPlease select a team for Outcome Assessment.');
TIDs[0].focus();
}
else top.location =
'/' +
els.AssessDatabase.value +
'/outcome?openform&TeamID=' +
vals.join(',');
}

</script>
</head>
<body>
<form>
<input type="hidden" name="AssessDatabase" value="somedB">
<br><br>
<input type="checkbox" id="TID1" name="TeamID" value="ID1">
<label for="TID1">ID1</label>
<br>
<input type="checkbox" id="TID2" name="TeamID" value="ID2">
<label for="TID2">ID2</label>
<br>
<input type="checkbox" id="TID3" name="TeamID" value="ID3">
<label for="TID3">ID3</label>
<br><br>
<input
type="button"
value="process"
onclick="return processOutcome(this.form.elements)">
</form>
</body>
</html>

Q: why not just do this as a normal form submission? At least you'd get
data posted without the intervention of JavaScript.

Jul 23 '05 #5
ef*****@epitome.com.sg wrote:
I have a page with checkboxes generated by a script. Users are suppose
to select the checkboxes and click on a button to process their
selection. The codes work fine except when I only have one checkbox on
the page. Then it will not register sReturn. Can anyone help me out,
why this occur when there is only 1 checkbox on the page.


IMO, people should _STOP_ using direct calls to form objects to get and set
values (except in rare situations).

Instead, you could do this:
sReturn = getInputValue(document.forms[0].TeamID);

Using the function from here:
http://www.javascripttoolbox.com/validations/

That would give you back a comma-separated list of values, whether there was
one checkbox or multiple.

Much easier, and less prone to error. IMO.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #6
Thanks everyone. I appreciate your inputs. In particular thank to Matt
for pointing out your function pages

Jul 23 '05 #7

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

Similar topics

3
by: Cary | last post by:
Hi all, I seem to be losing my mind here. I can't get this to work and I know it must be something stupid. I have an asp page with a html checkbox on it. I have a button that calls a...
5
by: Steve | last post by:
Hello, I've been a PHP programmer for a number of years and have just started to learn JS. My Employer (a water analysis lab) wants what should be a very simple .js written that basically takes...
0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
2
by: bienwell | last post by:
Hi, Does the OnSelectedIndexChanged event work when we mark on the checkbox on the cell of Datagrid control ? I tried to call this event; it didn't affect anything. Do you know any event that...
1
by: solomon_13000 | last post by:
connection.asp: <% Sub RunQueryString (pSQL,parms) on error resume next Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &...
1
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I...
2
by: dougloj | last post by:
Hi. I have an ASP.NET application written in C#. To log in, a user must provide their email address and password. I already give the user a "Remember my Email Address" check box. If they check...
11
by: giloosh | last post by:
i would like to filter out a bunch rows from a table of sql row results. i have a checkbox on each row named checkbox (x = the current row id) i will submit the form and take all the rows that...
1
by: shivasusan | last post by:
Hi Friends! Please Help me! How i can solve this error? Please explain me, what is the error and how to solve? Error Type: ADODB.Recordset (0x800A0E7D) The connection cannot be used to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.