IE/XP Problem setting option element to selected

T

thomastk

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>
 
R

RobG

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 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://groups.google.co.uk/group/co...on(+value+text&rnum=20&hl=en#07f4f9df6cd3cdc1>
 
A

ASM

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?

see there :
<http://aliasdmc.free.fr/dom_javascript_html/javascript_html_select_add.html>

and see also bellow old JS that works
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">

ha ! ben bravo !
<TITLE></TITLE>

function mytesting()
{
var fsDropDown = document.getElementById("select1");
if (fsDropDown !=null) {

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;
 
R

RobG

ASM wrote:
[...]
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;


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

[...]
 

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
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top