I am have used php to create a two sets of arrays of listboxes, each
of the listbox in the array have a unique ID. One of the list boxes
are dependant on the other one. I have written the code in javascript,
well changed existing code. I need to be able to select the element by
ID rather than name. The code was working before I changed things to
"getElementByID ". I am not very good at javascript so I am not sure if
I am on the right track, currently nothing is happening.
This is the Javascript code:
function swapName(form, i) {
var Type = 'lsType' + i;
var Name = 'lsName' + i;
Type = document.getEle mentByID(Type). options[document.getEle mentByID(Type). selectedIndex].value;
// this bit resets the name select list to nothing.
while (document.getEl ementByID(Name) .options.length > 1)
document.getEle mentByID(Name). options[0] = null;
// this bit populates the name select list with the required cities.
if (Type.length > 0) {
current_array=N ames[Type];
for (j=0;j<current_ array.length;j+ +) {
var optionName = new Option(current_ array[j], current_array[j],
false);
document.getEle mentByID(Name). options[document.getEle mentByID(Name). length]
= optionName;
}
}
}
This is the bit that calls it:
<select name="lsType[]" onChange="swapN ame(this.form,' .$i.')"
id="lsType'.$i. '">
Any suggestions would be welcome
Thanks in advance!
of the listbox in the array have a unique ID. One of the list boxes
are dependant on the other one. I have written the code in javascript,
well changed existing code. I need to be able to select the element by
ID rather than name. The code was working before I changed things to
"getElementByID ". I am not very good at javascript so I am not sure if
I am on the right track, currently nothing is happening.
This is the Javascript code:
function swapName(form, i) {
var Type = 'lsType' + i;
var Name = 'lsName' + i;
Type = document.getEle mentByID(Type). options[document.getEle mentByID(Type). selectedIndex].value;
// this bit resets the name select list to nothing.
while (document.getEl ementByID(Name) .options.length > 1)
document.getEle mentByID(Name). options[0] = null;
// this bit populates the name select list with the required cities.
if (Type.length > 0) {
current_array=N ames[Type];
for (j=0;j<current_ array.length;j+ +) {
var optionName = new Option(current_ array[j], current_array[j],
false);
document.getEle mentByID(Name). options[document.getEle mentByID(Name). length]
= optionName;
}
}
}
This is the bit that calls it:
<select name="lsType[]" onChange="swapN ame(this.form,' .$i.')"
id="lsType'.$i. '">
Any suggestions would be welcome
Thanks in advance!
Comment