Page 1 of 1
Find if control is an array
Posted: Thu Dec 23, 2004 7:54 am
by Stryks
Hi all,
This probrably seems like an odd question, but is there any way I can determine if a control on a form is an object or just a one of?
I am trying to loop through the controls on a form using this loop structure:
Code: Select all
for (i=0; i < currform.isSelected.length; i++) {
alert (currform.isSelectedїi].name)
}
The problem is that if there is only one of the control on the form then it just returns the length of the value, or in the case of the example, how many items there are in a listbox.
Any ideas? Thanks
Posted: Thu Dec 23, 2004 9:44 am
by Weirdan
perhaps you're looking for something like this:
Code: Select all
<form id='f1' action='' method='GET'>
<select name='c1' id='c1'>
<option>something</option>
<option>something else</option>
</select>
</form>
<input id='b1' type='button' value='Go' />
<script type='text/javascript' language='Javascript'>
function val(frm) {
for(i in frm.childNodes) {
if(!frm.childNodesїi].tagName) continue; // skip text nodes
alert(frm.childNodesїi].name);
}
}
document.getElementById('b1').onclick = new Function("evt",
"return val(document.getElementById('f1'));"
);
</script>
Posted: Thu Dec 23, 2004 3:11 pm
by Stryks
Hmmm .... perhaps this is something like what I am after, but I'm not sure if I can implement it in the way I would like.
Here is the scenario. I have a list of items displayed in a form, with a checkbox and two textboxes per row. The checkbox is used to tell if the row is to be used.
I also have a row underneath with the same textboxes, but with a listbox control instead of the checkbox in which a user can choose to create a sub item for one of the items selected in the list above.
The idea is that the listbox will be filled dynamically with items that have been ticked so that at any given time, the user can only choose items that are ticked.
Just because things are never that simple, these areas are repeated in the same form, so there will be a list of available rows in a category, then the addition row, then rows for the next category, etc.
I am trying to do this by looping through the checkboxes when one is clicked, and repopulating the listbox each time. The problem is that the amount of categories starts out as zero (which is fine) but when we are trying to add the first category, the select control is alone on the page and therefore not an array. So if I try to loop through it as per my previous post, it just returns the number of options in the select box instead of how many iterations of it there are, which should be 1 in this scenario. More than one and it works as expected.
Does this make sense? Is there any way around this?
Thanks
Posted: Thu Dec 23, 2004 5:06 pm
by Stryks
I have found a solution of sorts, though I am sure it is less glamourous than it could possibly be.
I just got PHP to set a hidden field called rowCount to tell me how many checkboxes and listboxes there are in the format of checkboxes,listboxes.
I then call the following on the onClick event of the checkboxes.
Code: Select all
function refreshZoneList(control) {
var index = parseInt(control.name.substring(control.name.indexOf("ї") + 1, control.name.length - 1))
var currform = control.form;
var params = currform.rowCount.value.split(",")
var checkVal
// Find the correct listbox
if(parseInt(paramsї1]) == 1) {
// there is only one of this control, access it directly
var targetList = currform.newRow
var startPoint = 0;
var endPoint = parseInt(currform.newRow.name.substring(currform.newRow.name.indexOf("ї") + 1, currform.newRow.name.length - 1)) -1
} else {
// loop through to find the next new row after the clicked checkbox
for (i=0; i < parseInt(paramsї1]); i++) {
checkVal = parseInt(currform.newRowїi].name.substring(currform.newRowїi].name.indexOf("ї") + 1, currform.newRowїi].name.length - 1));
var startPoint = 0;
if (index < checkVal) {
var targetList = currform.newRowїi];
var endPoint = checkVal-1;
break;
} else {
var startPoint = i+1;
}
}
}
// Clear the listbox
clearcombo(targetList)
//Get the checkbox values
if (parseInt(paramsї0]) == 1) {
// there is only one of this control, access it directly
if (currform.isSelected.checked) {
targetList.optionsїtargetList.options.length]= new Option("Create Sub-Zone in ...", "0");
targetList.optionsїtargetList.options.length]= new Option(currform.zonename.value, currform.isSelected.value);
}
} else {
// this is a control array, iterate instances between defined start and end points
for (i=startPoint; i < endPoint; i++) {
if (currform.isSelectedїi].checked) {
if (targetList.options.length == 0) {
targetList.optionsїtargetList.options.length]= new Option("Create Sub-Zone in ...", "0");
}
targetList.optionsїtargetList.options.length]= new Option(currform.zonenameїi].value, currform.isSelectedїi].value);
}
}
}
// Check to make sure that the list hasnt been left empty. Pad with value and disable if it is.
if (targetList.options.length == 0) {
targetList.optionsїtargetList.options.length]= new Option("Choose a Primary Zone", "0");
targetList.disabled = true
} else {
targetList.disabled = false
}
// force a refresh of row selection for the targetList so that it gets greyed
setRow(targetList);
}
Is it perfect? I seriously doubt it. But it seems to be working at this point.
If you can see any obvious way to do this a cleaner way, let me know. I'm still trying to wrap my mind around the whole javascript thing, and any tips are appreciated.
Thanks