Dynamically dependant listBoxes

N

nadia

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.getElementByID(Type).options[document.getElementByID(Type).selectedIndex].value;

// this bit resets the name select list to nothing.
while (document.getElementByID(Name).options.length > 1)
document.getElementByID(Name).options[0] = null;
// this bit populates the name select list with the required cities.
if (Type.length > 0) {
current_array=Names[Type];

for (j=0;j<current_array.length;j++) {
var optionName = new Option(current_array[j], current_array[j],
false);
document.getElementByID(Name).options[document.getElementByID(Name).length]
= optionName;
}
}
}

This is the bit that calls it:
<select name="lsType[]" onChange="swapName(this.form,'.$i.')"
id="lsType'.$i.'">

Any suggestions would be welcome
Thanks in advance!
 
R

Richard Cornford

This is the Javascript code:
function swapName(form, i) {
var Type = 'lsType' + i;
This is the bit that calls it:
<select name="lsType[]" onChange="swapName(this.form,'.$i.')"
id="lsType'.$i.'">
<snip>

When you build the Type local variable to use as the ID you are not
including the two single quote characters that you have used in the ID
attribute string. Those single quotes are illegal characters in HTML
terms, as is the $ and the [ and ] in the name attribute so even if you
do include them in when building the Type local variable you should not
have any expectation of the result working on more than just some (if
common) HTML browsers.

From the HTML specification:-
<quote>
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").
</quote>

Richard.
 
L

Lasse Reichstein Nielsen

The code was working before I changed things to
"getElementByID".

Then it is probably because it is with a lower case "d":
document.getElementById
^
This is the Javascript code:
function swapName(form, i) {
var Type = 'lsType' + i;
var Name = 'lsName' + i;
Type = document.getElementByID(Type).options[document.getElementByID(Type).selectedIndex].value;

I would split this into two lines:
var selElem = document.getElementById(Type);
var selValue = selElem.options[selElem.selectedIndex].value;

(purely for style, I don't want to reuse variables more than necessary).
<select name="lsType[]" onChange="swapName(this.form,'.$i.')"
id="lsType'.$i.'">

I assume this is part of a PHP string declaration, and the the value
of $i is concatenated with the rest of the string. Be aware that not
everybody in this newsgroup knows PHP (I know only a little), and that
the PHP code used to generate the page really isn't interesting to us.
What is interesting is the HTML that is generated, since that is where
the bug is.

/L
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top