By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,439 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,439 IT Pros & Developers. It's quick & easy.

Won't work in IE

P: n/a
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.
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ns***@student.cpit.ac.nz (nadia) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<vf**********@hotpop.com>...

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
Jul 20 '05 #3

P: n/a
ns***@student.cpit.ac.nz (nadia) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
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>
Jul 20 '05 #5

P: n/a
Replace line:

Type = selElem.options[selIndex].value;

By

Type = selElem.options[selIndex].text;
I hope this helps.

Wagner
Jul 20 '05 #6

P: n/a
Thanks, Works fine.

Thought it was something simple
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.