473,406 Members | 2,698 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,406 software developers and data experts.

Dynamic <select multiple> element problems

I have written Javascript which populates a form with <select> elements
dynamically based on some other stuff going on in the page. The
problem is that I can't get it to work properly in IE, and the closest
I can get to it working doesn't work in FireFox.

The first thing IE does wrong is that it only selects the last <option>
even though the multiple attribute is set on the <select>.
The other thing it does wrong is that it doesn't display the text that
has been assigned to the text attribute of the <option>.

Here's a relevant mskb article which doesn't solve my problem:
http://support.microsoft.com/default...78&Product=iep

Here's information about the add() DOM level 2 function:
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-14493106

Here's a file demonstrating the problem:
<html>
<body>
<a href="#" onclick="return clicker(false);">only selects last element
(bad/unexpected)</a><br/>
<a href="#" onclick="return clicker(true);">alert somehow causes IE to
select all elements</a>
<form id="meow">

</form>
<script type="text/javascript">
function clicker(alertit)
{
var theform = document.getElementById("meow");
var myselect = document.createElement("SELECT");
var optionTemplate = new Option("blank","blank");

alert(optionTemplate.text);

for(var i = 0; i < 5; i++)
{
optionTemplate.text = String(i);
optionTemplate.value = i;
//myselect.appendChild(optionTemplate.cloneNode(true )); //appendChild
doesnt work right in IE
//myselect.add(optionTemplate.cloneNode(true), null); //works in FF,
not in IE (type mismatch)
myselect.add(optionTemplate.cloneNode(true)); //works in IE, not in
FF (not enough arguments)
}

theform.appendChild(myselect);

myselect.multiple = true;

for(var i = 0; i < 5; i++)
{
if(alertit)
alert(i);
myselect.options[i].selected = true;
}

return false;
}

</script>
</body>
</html>

Jun 28 '06 #1
2 3096
> "re*******@gmail.com" <re*******@gmail.com> wrote:
news:11**********************@p79g2000cwp.googlegr oups.com....

I have written Javascript which populates a form with <select>
elements dynamically based on some other stuff going on in the
page. The problem is that I can't get it to work properly in IE,
and the closest I can get to it working doesn't work in FireFox.

[snip]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
var mySelect;
try{
mySelect=
document.createElement('<select name="willie" size="3" multiple><\/select>');
}
catch(myError){mySelect=null;}
if(!mySelect || !mySelect.name){
mySelect=document.createElement('select');
mySelect.name='willie';
mySelect.size='3';
mySelect.multiple=true;
}
function mSelect(elm){
elm.firstChild.data=(elm.firstChild.data=='add select')?
'remove select':
'add select';
var theform = document.forms.meow;
mySelect.options.length=0;
if(theform.elements.willie){
var elmSelect=theform.elements.willie;
elmSelect.parentNode.removeChild(elmSelect);
elmSelect=null;
return;
}
for(var i = 0; i < 5; i++){
mySelect.options[i]=new Option(i,i);
mySelect.options[i].selected = true;
}
document.getElementById('formDiv').appendChild(myS elect);
}
</script>
<title></title>
<meta http-equiv="content-type" content=
"text/html; charset=windows-1252">
</head>
<body>
<div>
<span onclick="mSelect(this);" style=
"cursor:pointer;color:blue;">add select</span>
</div>
<form action="javascript:void(this)" id="meow">
<div id="formDiv"></div>
</form>
</body>
</html>

--
BootNic Thursday, June 29, 2006 4:47 AM

Good communication is as stimulating as black coffee and just as hard
to sleep after.
*Anne Morrow Lindbergh*

Jun 29 '06 #2

BootNic wrote:
"re*******@gmail.com" <re*******@gmail.com> wrote:
news:11**********************@p79g2000cwp.googlegr oups.com....

I have written Javascript which populates a form with <select>
elements dynamically based on some other stuff going on in the
page. The problem is that I can't get it to work properly in IE,
and the closest I can get to it working doesn't work in FireFox.

[snip]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript">
var mySelect;
try{
mySelect=
document.createElement('<select name="willie" size="3" multiple><\/select>');
}
catch(myError){mySelect=null;}
if(!mySelect || !mySelect.name){
mySelect=document.createElement('select');
mySelect.name='willie';
mySelect.size='3';
mySelect.multiple=true;
}


Creating the select with document.createElement('<select name=""
size="" multiple<\/select>'); in IE solved the problem. Thanks for
your help!

Jun 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses...
3
by: Dennis M. Marks | last post by:
I have a problem with the following code. It generates a <FORM><SELECT><OPTION> list. There is no problem in the generating. The problem is in the execution as follows. It works fine in Mac IE...
3
by: gekoblu | last post by:
Hi!, I want to fix via javascript the combo width to a fix value. I'd like to implement a kind of ALT / TITLE function to show the entire option when the text is longer than the combo width......
2
by: abs | last post by:
Hello everybody. A piece of html code: <form ... > <select ....> <option .... <option .... </select> <select ....>
5
by: Brian Foley | last post by:
Hello, I am used to using the label tag with check boxes and radio buttons in html forms. This allows me to click on the text of the label to activate/deactivate the check box / button, rather...
1
by: Mike | last post by:
Hello, I have an ASP page that contains a custom control that has a <select> element. I would like to add some <option> elements dynamically from the ASP page. How can I do that? Thanks Mike
6
by: joseph.lindley | last post by:
Forgive me for I am a bit of a web-dev novice - but I'm not doing too bad. I'm currently working with a bit of javascript to dynamically add <option>s into a select box. My code currently works...
7
by: Alex Maghen | last post by:
I have some client-side JavaScript that I want to run whenever a pulldown <SELECT> is changes on th client. I'm trying to do this as follows... <select id="MyPulldown"...
7
by: lambertb | last post by:
Hi, is this possible to achieve this, and how? http://img217.imageshack.us/img217/779/derrrvw2.png thanks!
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.