...

View Full Version : Forms and Checkboxes, arrays



tcuos
12-10-2004, 01:31 PM
Hi,

as many before me I am very new at this. But hopefully someone out ther can help :-)

I have the following script:



<script language="javascript">
<!--
function check() {
var theform;
var list = "";
var first = true;

//browser detection
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.__aspnetForm;
}
else {
theform = document.forms["__aspnetForm"];
}



for(var i=0; i<theform.elements["box"].length; i++)
{
if(theform.elements["box"][i].checked)
{
if(first)
{
list = "?"+list+"sem"+i+"="+theform.elements["box"][i].value;
first = false;
}
else
{
list = list+"&sem"+i+"="+theform.elements["box"][i].value;
}
}
}


if(!first)
{
list = "<%=RegisterPageName()%>"+list;
}
else
{
list = "<%=CurrentPageName()%>";
}

return list;
}
// -->
</script>

This script will traverse a form containing checkboxes and get values based on the checkboxes that are checked.

The problem with this script is that when the form only has one element, which is a checkbox, it will not return the string with the value. But for 2 or more checkboxes it works OK. Why? ? ?

I can't say I fully understand the script either :-)

Appreciate any input, anyone.

Thx,

tcuos

requestcode
12-10-2004, 05:49 PM
That is because if you only have one element then you don't have an array or at least that is how I understand it. Try replacing the part of your code where the for loop is with this code that checks to see if ther are more than one elemens and if it there is it will do the for loop if not then it will just check the one element. Hope this helps and good luck.


if(theform.elements["box"].length>1)
{
for(var i=0; i<theform.elements["box"].length; i++)
{
if(theform.elements["box"][i].checked)
{
if(first)
{
list = "?"+list+"sem"+i+"="+theform.elements["box"][i].value;
first = false;
}
else
{
list = list+"&sem"+i+"="+theform.elements["box"][i].value;
}
}
}
}
else
{
if(theform.elements["box"][0].checked)
{
list = "?"+list+"sem"+i+"="+theform.elements["box"][0].value;
first = false;
}
}

tcuos
12-13-2004, 09:19 AM
Hi requestcode,

thank you for replying.

I pasted the code you provided and it seems that you are correct. The code behaves just as before when there are more than one checkboxes. Now however, I get a prompt about a runtime error, and if I want to Debug:

"Error: 'elements.box.0.checked' is null or not an object"

Any ideas? ? ?

/tcuos

tcuos
12-13-2004, 10:53 AM
Hello again,

I amended the code, replacing ["box"][0] with only ["box"]:


else //when there is only one checkbox and if this is checked
{
if(theform.elements["box"].checked)
{
list = "?"+list+"sem"+i+"="+theform.elements["box"].value;
first = false;
}
}


I am not sure why it works now, but it does :-)

Thank you for your clue!

/Tcuos

glenngv
12-13-2004, 01:14 PM
//browser detection
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.__aspnetForm;
}
else {
theform = document.forms["__aspnetForm"];
}


FWIW, you don't have to do the above detection. Both ways are correct for any browser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum