Dynamic <select multiple> element problems

R

rehevkor5

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.aspx?scid=kb;en-us;298978&Product=iep

Here's information about the add() DOM level 2 function:
http://www.w3.org/TR/DOM-Level-2-HTML/html.html#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.selected = true;
}

return false;
}

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

BootNic

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=new Option(i,i);
mySelect.options.selected = true;
}
document.getElementById('formDiv').appendChild(mySelect);
}
</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*
 
R

rehevkor5

BootNic said:
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!
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top