IE/XP Problem setting option element to selected

IE/XP Problem setting option element to selected

Post by thomast » Fri, 02 Sep 2005 08:52:18


Hi,
In the following script, I am trying to set selection to a select
option element, that is newly created within the script. It works fine
on IE installations on Windows 2000 and some XP machines. But on some
XP machines, the selection doesn't happen and it defaults to the first
element in the options array. Has anybody come across this problem ?
Any known workarounds?

Thanks
Thomas.
===================================

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
</HEAD>
<BODY>

<P><INPUT id=text1 name=text1 onchange="javascript:mytesting(this);">
<SELECT id=select1 style="WIDTH: 264px" name=select1>
<OPTION selected value="testing 1">testing 1</OPTION>
<OPTION value="testing 2">testing 2</OPTION>
<OPTION value="testing 3">testing 3</OPTION>
<OPTION value="testing 4">testing 4</OPTION>
<OPTION value="testing 5">testing 5</OPTION>
</SELECT><INPUT id=button1 type=button value="delect new added"
name=button1></P>
</BODY>
</HTML>
<script language="JavaScript" >
function mytesting()
{
var fsDropDown = document.getElementById("select1");
if (fsDropDown !=null) {
var tempFS = fsDropDown.value;
//var selIdx = fsDropDown.selectedIndex;
var opt = document.createElement("OPTION");
fsDropDown.options.add(opt);
tempFS = tempFS+"*";
opt.innerText = tempFS;
opt.value = tempFS;
//opt.selected = true;

fsDropDown.options[fsDropDown.options.length-1].selected=true;
fsDropDown.selectedIndex = fsDropDown.options.length-1;
}
}
</script>
 
 
 

IE/XP Problem setting option element to selected

Post by RobG » Fri, 02 Sep 2005 09:53:39


IE has problems with options. To set all the properties on one go, use
something like:

var opt = new Option( text, value, defaultSelected, currentSelected);

where:
text is the option text as a string,
value is the option value as a string,
defaultSelected is a boolean (true or false),
currentSelected is a boolean (true or false),

There is a thread worth reading here:

<URL: http://www.yqcomputer.com/ +option(+value+text&rnum=20&hl=en#07f4f9df6cd3cdc1>




--
Rob

 
 
 

IE/XP Problem setting option element to selected

Post by ASM » Fri, 02 Sep 2005 10:31:05


see there :
< http://www.yqcomputer.com/ ;

and see also bellow old JS that works


ha ! ben bravo !


<script type="text/javascript">


var tempFS = fsDropDown.options[fsDropDown.selectedIndex].value;
fsDropDown.length++;
var opt = fsDropDown.options[fsDropDown.length-1]
opt.value = opt.text = tempFS = tempFS+"*";
opt.selected = 'selected';
if(document.all) opt.selected = true;

> </HTML>

--
Stephane Moriaux et son [moins] vieux Mac
 
 
 

IE/XP Problem setting option element to selected

Post by RobG » Fri, 02 Sep 2005 10:59:36


[...]


var tempFS = fsDropDown.options[fsDropDown.selectedIndex].value;
var newOpt = new Option( tempFS+'*', tempFS+'*', false, true );
fsDropDown.options[fsDropDown.options.length] = newOpt;

[...]

--
Rob
 
 
 

IE/XP Problem setting option element to selected

Post by thomast » Sat, 03 Sep 2005 08:01:17

Tried both the suggestions, but the problem is still reproducible on
the problem XP machines, as with the js/html I had posted.

Thanks
Thomas.