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

E

effendi

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.checked)
if (sReturn=="")
sReturn=oCheckboxs.value;
else
sReturn=sReturn+","+oCheckboxs.value;
}
if (sReturn=="") {
alert("Select a team for Outcome Assessment"); }
else {
window.location="/"+mainDatabase+"/outcome?openform&TeamID=" +sReturn}

}
 
R

RobB

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.checked)
if (sReturn=="")
sReturn=oCheckboxs.value;
else
sReturn=sReturn+","+oCheckboxs.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.checked)
........
 
D

Dietmar Meier

(e-mail address removed) wrote:

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


with

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

ciao, dhgm
 
R

RobB

Dietmar said:
(e-mail address removed) wrote:

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


with

var aReturn = [];
for (var i=0; i<oCheckboxs.length; i++) {
if (oCheckboxs.checked) {
aReturn[aReturn.length] = oCheckboxs.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;
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.
 
M

Matt Kruse

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.
 
E

effendi

Thanks everyone. I appreciate your inputs. In particular thank to Matt
for pointing out your function pages
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top