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

IE/XP Problem setting option element to selected

P: n/a
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>

Sep 1 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
th******@hotmail.com wrote:
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/comp.lang.javascript/browse_frm/thread/f1037605dfb1a9c0/07f4f9df6cd3cdc1?q=new+option(+value+text&rnum=20& hl=en#07f4f9df6cd3cdc1>


--
Rob
Sep 1 '05 #2

P: n/a
ASM
th******@hotmail.com wrote:
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>
<script type="text/javascript">
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;
}
}
</script>
</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>


--
Stephane Moriaux et son [moins] vieux Mac
Sep 1 '05 #3

P: n/a
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;

[...]

--
Rob
Sep 1 '05 #4

P: n/a
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.

Sep 1 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.