473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

IE/XP Problem setting option element to selected

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
4 2480
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: netclectic | last post by:
I'm dynamically adding options to a select list in javascript and i need to be able to set the height of the option, but setting style.height has not effect, I also tried style.pixelHeight but no...
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
2
by: gabon | last post by:
I'm creating a select entirely through JavaScript and very strangely IE doesn't show the text in the option elements. Here part of the code: this.form_country=document.createElement("select");...
7
by: Stefan Mueller | last post by:
I choose 'Entry 4' and click then on the button 'Set' to set the index to 'Entry 2'. If you press now the cursor down key 'Entry 5' instead of 'Entry 3' shows up with Mozilla Firefox. With Internet...
1
by: Brent | last post by:
I thought this would be a problem easily fixed in a simple Google search, but I'm having a hard time finding a solution. I simply need a way to set the value of an option element from an option...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
6
by: Charleees | last post by:
Hi all, I have a DropDown and a TextBox just bekeow it... I have to get the selected value from dropdown and set it as textBox Text.. The thing is i have to do this Without PostBack..... ...
7
by: prash.marne | last post by:
Hello, I have a simple form <form method="POST"> <select name="activity"> <option value="0">None</option> <option value="M" onclick="popup_onclick()">Select Multiple</option> <option...
2
by: Sebastian Fey | last post by:
hi, how do you select an option using javascript? i tried to setAttribute("selected", "disabled") to disable last selected and setAttribute("selected", "selected") for the newly selected. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.