Hi,
The following JS code (supplied by 'Jofa', thanks) is working okay, but there are just a few things I don't understand, so if a JS guru could explain please:
What does the following do ?Code:<script language="javascript"> function CategoryGroup(id, name) { this.id = id; this.name = name; } var CategoryGroupList = new Array(); CategoryGroupList[0] = new CategoryGroup("-1","Select Category Group"); CategoryGroupList[1] = new CategoryGroup("1","Hardware"); // ..etc,etc function Category(group, name, categoryid) { this.group = group; this.name = name; this.categoryid = categoryid; } var CategoryList = new Array(); CategoryList[0] = new Category("-1","Select Category","-1"); CategoryList[1] = new Category("2","Accounting","25"); // etc,etc function SubCategory(categoryid, subcatname, subcatid) { this.categoryid = categoryid; this.subcatname = subcatname; this.subcatid = subcatid; } var SubCategoryList = new Array(); SubCategoryList[0] = new SubCategory("-1","Select Sub Category","-1"); SubCategoryList[1] = new SubCategory("27","Business Package","5"); //etc,etc function getCategories(group) { var category = CategoryGroupList[group].id var current = (frm.selCategory.value) ? CategoryList[frm.selCategory.value].name : ''; for(z = frm.selCategory.length - 1; z >= 0; z--) { frm.selCategory.options[z] = null; } for(z = 0; z < CategoryList.length; z++) { var prod = CategoryList[z]; if(prod.group == category) { frm.selCategory.options[frm.selCategory.options.length] = new Option(prod.name, z, (prod.name==current), (prod.name==current)); } } frm.categorygroup.value = CategoryGroupList[frm.selGroup.value].id; } </script>
In the following code, does the FOR line mean z is set to the current number of options in the dropdown list, less one. Then the FOR (loop) will continue as long as z >=0 , and the decrement for each iteration is done by z-- ??Code:var current = (frm.selCategory.value) ? CategoryList[frm.selCategory.value].name : '';
Basically, the FOR loop removes all OPTIONS in the (SELECT) dropdown list, ...... right ??
Why is the following JS necessary when populating the dropdown list (SELECT/OPTIONS) ?Code:for(z = frm.selCategory.length - 1; z >= 0; z--) { frm.selCategory.options[z] = null; }
That is, there seem to be 4 elements placed in _this_ index of the OPTIONS, when if the OPTIONS are built from hard code, as follows:Code:frm.selCategory.options[frm.selCategory.options.length] = new Option(prod.name, z, (prod.name==current), (prod.name==current));
there are only 2 elements necessary. The second method would appear to be more in line with (HTML) code that is completely hard coded for OPTIONS, such as, only needing a 'value' and 'description/name', like:Code:frm.SelGroup.options[frm.SelGroup.options.length] = new Option('Hardware', '1');
I just can't see what the extra 2 elements ( (prod.name==current), (prod.name==current) do ??Code:<option VALUE="1">Hardware</option>
Thanks,
Peter








Bookmarks