473,698 Members | 2,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById("meow" );
var myselect = document.create Element("SELECT ");
var optionTemplate = new Option("blank", "blank");

alert(optionTem plate.text);

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

theform.appendC hild(myselect);

myselect.multip le = true;

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

return false;
}

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

Jun 28 '06 #1
2 3105
> "re*******@gmai l.com" <re*******@gmai l.com> wrote:
news:11******** **************@ p79g2000cwp.goo glegroups.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.create Element('<selec t name="willie" size="3" multiple><\/select>');
}
catch(myError){ mySelect=null;}
if(!mySelect || !mySelect.name) {
mySelect=docume nt.createElemen t('select');
mySelect.name=' willie';
mySelect.size=' 3';
mySelect.multip le=true;
}
function mSelect(elm){
elm.firstChild. data=(elm.first Child.data=='ad d select')?
'remove select':
'add select';
var theform = document.forms. meow;
mySelect.option s.length=0;
if(theform.elem ents.willie){
var elmSelect=thefo rm.elements.wil lie;
elmSelect.paren tNode.removeChi ld(elmSelect);
elmSelect=null;
return;
}
for(var i = 0; i < 5; i++){
mySelect.option s[i]=new Option(i,i);
mySelect.option s[i].selected = true;
}
document.getEle mentById('formD iv').appendChil d(mySelect);
}
</script>
<title></title>
<meta http-equiv="content-type" content=
"text/html; charset=windows-1252">
</head>
<body>
<div>
<span onclick="mSelec t(this);" style=
"cursor:pointer ;color:blue;">a dd select</span>
</div>
<form action="javascr ipt: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*******@gmai l.com" <re*******@gmai l.com> wrote:
news:11******** **************@ p79g2000cwp.goo glegroups.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.create Element('<selec t name="willie" size="3" multiple><\/select>');
}
catch(myError){ mySelect=null;}
if(!mySelect || !mySelect.name) {
mySelect=docume nt.createElemen t('select');
mySelect.name=' willie';
mySelect.size=' 3';
mySelect.multip le=true;
}


Creating the select with document.create Element('<selec t 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
2291
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 these lines (full text is below): if (document.form1.theDay.options.text != "Wednesday) { var days = document.form1.theDay; days.options.text = days.options.text; <snip> var option - new Option("Wednesday", 2);
3
1585
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 with and without the <FORM></FORM>. In Mac Netscape 6 the onChange does not activate if there is no <FORM></FORM>. When I put it in a form as indicated below the value of train.options.value is undefined. What am I doing wrong?
3
16434
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... It's possible?!? Thanks
2
1732
by: abs | last post by:
Hello everybody. A piece of html code: <form ... > <select ....> <option .... <option .... </select> <select ....>
5
8310
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 than having to click on the (possibly small) box or button. I recently tried to assign a label to a select "drop down list", but found that when I clicked on the label text to bring it into focus, the select box was reset to the first entry. ...
1
1112
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
19602
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 fine in Internet Explorer, however in Firefox the dropdown only displays the first option in the list, and when clicked the other values aren't displayed. Here is the code;
7
6279
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" onchange="handleProblemChange();" runat="server"> If I do this, I get client-side javascript errors because the id "MyPulldown" doesn't exist. It's been mangled into this crazy-named thing that I can't use. How do I fix this so that the ID that I give it ends up being...
7
10916
by: lambertb | last post by:
Hi, is this possible to achieve this, and how? http://img217.imageshack.us/img217/779/derrrvw2.png thanks!
11
4503
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 rows inserted/sec up to 500, and 100rows/6secs up to 3000, which isn't great but then the Row Count is clicking away for the user to see and they can hit the "cancel" button at anytime, so overall I'm happy), what really disappoints me is the...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.