Won't work in IE

N

nadia

There is probably a easy answer to this. I want to get the value of
the selection in a listbox. The following code works happily in
mozilla but not in IE.

var selElem = window.document.getElementById(Type);
//Type is a variable name that is the same as whats on
the form
var selIndex = selElem.selectedIndex;
Type = selElem.options[selIndex].value;

I think its the last line thats the problem.

Thanks all.
 
L

Lasse Reichstein Nielsen

There is probably a easy answer to this. I want to get the value of
the selection in a listbox. The following code works happily in
mozilla but not in IE.

Which IE?
var selElem = window.document.getElementById(Type);
//Type is a variable name that is the same as whats on
the form
var selIndex = selElem.selectedIndex;
Type = selElem.options[selIndex].value;

I think its the last line thats the problem.

I don't. It looks fine to me.

What error message do you get (not that IE's error messages are that good)?
Are you sure that selElem is found correctly?

Where is Type set and used?

/L
 
N

nadia

Lasse Reichstein Nielsen said:
Which IE?

6.0 (I haven't checked with other versions)
var selElem = window.document.getElementById(Type);
//Type is a variable name that is the same as whats on
the form
var selIndex = selElem.selectedIndex;
Type = selElem.options[selIndex].value;

I think its the last line thats the problem.

I don't. It looks fine to me.

What error message do you get (not that IE's error messages are that good)?
Are you sure that selElem is found correctly?

It doesn't give me any error messages, When I tried to debug it
selIndex returned the correct number. Type just returns nothing.
Where is Type set and used?
Type is set just above the code with :
var Type = 'lsType' + i; //i is a number passed in to the function to
identify
//the unique id of a list box
:=) Nadia
 
L

Lasse Reichstein Nielsen

Type = selElem.options[selIndex].value;
I think its the last line thats the problem.
Where is Type set and used?
Type is set just above the code with :
var Type = 'lsType' + i; //i is a number passed in to the function to
identify
//the unique id of a list box

So it is a local variable, only visible inside the function. You then
set it to the value of the selected option of the select element ...
and where is it used? Do you return the value?

/L
 
N

nadia

This is the full code:

Javascript in the header:
<script type="text/javascript" language = "javascript">
Names = new Array();
Names["Browser"] = ['Any', 'Netscape Communicator', 'Ms-internet
explorer', 'Opera', 'Amaya', 'Mozilla'];
Names["Operating System"] = ['pc-dos', 'ms-windows', 'macOS', 'unix',
'muli-os', 'none'];
Names["none"] = ['none'];

function swapName(form, i) {
var Type = 'lsType' + i;
var Name = 'lsName' + i;

//select the type lsbox
var selElem = window.document.getElementById(Type);
var selIndex = selElem.selectedIndex;
//get the value of the selected item in the lsType box
Type = selElem.options[selIndex].value;

//select the Name lsBox
selElem = window.document.getElementById(Name);
// this bit resets the name select list to nothing.
while (selElem.options.length > 0) selElem.options[0] = null;

// this bit populates the name select list with the required Names.
if (Type.length > 0) {

var current_array = Names[Type];
for (j=0;j<current_array.length;j++) {

var optionName = new Option(current_array[j],
current_array[j], false, false);
//var optionName = new Option('hello', 'hello', false, false);
selElem.options[selElem.options.length] = optionName;
}
}
}
</script>

HTML in the body (generated from a php script):
<select name="lsType[]" onChange="swapName(this.form,0)" id="lsType0">
<option>none
<option>Operating System
<option>Browser
</select>
<select name="lsName[]" id="lsName0"></select>
 
W

W d'Anjos

Replace line:

Type = selElem.options[selIndex].value;

By

Type = selElem.options[selIndex].text;


I hope this helps.

Wagner
 

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

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top