473,508 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding items to a dropdown - Compatibility issue

The code below appears to work on the following:

MAC - Safari
PC - IE
PC - Opera

But the addition of items to the dropdown (select2) does not function
in:

MAC – IE
PC – Mozilla

I am hoping someone can point me in the right direction as to why this
is not occurring.

Any help is much appreciated,
Scott

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

var listHTML = "<TABLE width=100% border=0 cellspacing=1
cellpadding=3>" +
" <tr>" +
" <td width=100>Listbox:</td>" +
" <td><SELECT id=select2 name=select2></SELECT></td>" +
" </tr>" +
"</TABLE>"

var textHTML = "<TABLE width=100% border=0 cellspacing=1
cellpadding=3>" +
" <tr>" +
" <td width=100>Textbox:</td>" +
" <td><INPUT type=text id=text3 name=text3 size=10
maxlength=10></td>" +
" </tr>" +
"</TABLE>"

function ShowHide()
{
var myForm = document.workback;
var mySel = myForm.select1;
var myDiv = document.getElementById("optSel");
if(mySel.value==1)
{
myDiv.innerHTML = textHTML;
}
else
{
myDiv.innerHTML = listHTML;
var myOption;
var myList = myForm.select2;
for(i=1;i<=20;i++)
{
myOption = document.createElement("Option");
myOption.text = i;
myOption.value = i;
myList.add(myOption);
}
}
}

</SCRIPT>
</HEAD>
<BODY onload="ShowHide();">
<FORM name="workback">
<TABLE width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width=100>Show:</td>
<td>
<SELECT id=select1 name=select1 onchange="ShowHide();">
<OPTION value=0>Listbox</OPTION>
<OPTION value=1>Textbox</OPTION>
</SELECT>
</td>
</tr>
</TABLE>
<div id="optSel"></div>
</FORM>
</BODY>
</HTML>
Jul 23 '05 #1
6 8056
Scott wrote:
The code below appears to work on the following:

MAC - Safari
PC - IE
PC - Opera

But the addition of items to the dropdown (select2) does not function
in:

MAC – IE
PC – Mozilla

I am hoping someone can point me in the right direction as to why this
is not occurring.

Any help is much appreciated,
Scott
Please don't post code with tabs, use 2 (preferred) or 4 spaces for
indents. Manually wrap code at about 70 characters so autowrapping
doesn't introduce more errors than you had to start with.

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
The language attribute is depreciated, type is required:

<script type="text/javascript">

var listHTML = "<TABLE width=100% border=0 cellspacing=1
cellpadding=3>" +
You probably should quote your attribute values - it isn't mandatory
but you've done it elsewhere in your HTML.

[...]
for(i=1;i<=20;i++)
{
myOption = document.createElement("Option");
myOption.text = i;
myOption.value = i;
myList.add(myOption);
myList.appendChild(myOption);
}
}
}

</SCRIPT>
</HEAD>
<BODY onload="ShowHide();">
<FORM name="workback">
<TABLE width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width=100>Show:</td>
<td>
<SELECT id=select1 name=select1 onchange="ShowHide();">
<OPTION value=0>Listbox</OPTION>
<OPTION value=1>Textbox</OPTION>
</SELECT>
</td>
</tr>
</TABLE>
<div id="optSel"></div>
</FORM>
</BODY>
</HTML>

--
Rob
Jul 23 '05 #2
RobG wrote:
<snip>
for(i=1;i<=20;i++)
{
myOption = document.createElement("Option");
myOption.text = i;
myOption.value = i;
myList.add(myOption);


myList.appendChild(myOption);

<snip>

Out of the frying pan and into the fire. Where adding option elements to
a select element is concerned DOM is a nightmare.

But one simple (and possibly unpopular) truth is that the old way still
works on the newest browsers, reliably:-

myOption = new Option(i, i, true, true);
myList.options[myList.options.length] = myOption;

- and I only know of only two browsers where that doesn't work; NetFront
4/Web Browser 2, which has no Option constructor (and no alternative
method is supported either, it is just not that dynamic) and Opera 5.02,
where the - options - collection lacked a - length - property (but a bit
if feature testing would find the viable - length - property on the
SELECT element, though it is unlikely that version of Opera is still in
use anywhere).

When you have a one-code-fits-all approach available incompatibility for
the sake of DOM evangelism doesn't seem that rational.

Richard.
Jul 23 '05 #3
Worked like a charm, thanks Richard!

Scott

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #4
Richard Cornford wrote:
RobG wrote:
<snip>
for(i=1;i<=20;i++)
{
myOption = document.createElement("Option");
myOption.text = i;
myOption.value = i;
myList.add(myOption);


myList.appendChild(myOption);


<snip>

Out of the frying pan and into the fire. Where adding option elements to
a select element is concerned DOM is a nightmare.

But one simple (and possibly unpopular) truth is that the old way still
works on the newest browsers, reliably:-

myOption = new Option(i, i, true, true);
myList.options[myList.options.length] = myOption;

- and I only know of only two browsers where that doesn't work; NetFront
4/Web Browser 2, which has no Option constructor (and no alternative
method is supported either, it is just not that dynamic) and Opera 5.02,
where the - options - collection lacked a - length - property (but a bit
if feature testing would find the viable - length - property on the
SELECT element, though it is unlikely that version of Opera is still in
use anywhere).

When you have a one-code-fits-all approach available incompatibility for
the sake of DOM evangelism doesn't seem that rational.


I guess the obvious question is: which mainstream browsers don't
support the DOM appendChild method for option elements?

My intention wasn't to evangelise DOM, only to suggest a standards
compliant method that works. I didn't test my solution on IE for
Mac, but it did work in Mozilla for PC, the OP's other required
platform.

AFAICR, IE on Mac has some foibles in this area, but adding options
using DOM isn't one of them.

I intended testing the 'incrementing-the-options-length' method, but
ran out of time - unfortunately I have to compete for Mac-time.
--
Rob
Jul 23 '05 #5
Just another quick note:

When testing the updated code some interesting things were happening
on the MAC, with both Safari and IE. Sometimes the listbox would be
populated and other times it wouldn't, the listbox even appeared
disabled at times. To correct this problem, I modified the following
line:

var myList = myForm.select2;

is now:

var myList = document.getElementById("select2");

I have fully tested it on all the mentioned platforms and this has
resolved my compatibility issue, thanks to all!

Scott
Jul 23 '05 #6
RobG wrote:
Richard Cornford wrote:
RobG wrote: <snip>
for(i=1;i<=20;i++)
{
myOption = document.createElement("Option");
myOption.text = i;
myOption.value = i;
myList.add(myOption);

myList.appendChild(myOption);
<snip> When you have a one-code-fits-all approach available
incompatibility for the sake of DOM evangelism doesn't
seem that rational.
I guess the obvious question is: which mainstream browsers
don't support the DOM appendChild method for option elements?


Why would it matter how 'mainstream' a browser is when there is a method
that will work reliably on all the browsers where the action is
possible, and is amenable to reliable feature detection to determine its
viability?

But IE 5.0 had an interesting problem with - appendChild - when applied
to SELECT elements; it crashed. And as the SELECT elements had an -
appendChild - method and the creation of OPTION elements with -
createElement - was supported there was not a lot that could be feature
tested to avoid provoking the crash.

But on IE 5.5+ you will not find your proposed formulation of:-

myOption = document.createElement("Option");
myOption.text = i;
myOption.value = i;
myList.appendChild(myOption);

- entirely satisfactory.
My intention wasn't to evangelise DOM,
I did not intend to imply that you were. But there are people who will
promote particular scripted formulations because they are "DOM standard"
regardless (though possibly in ignorance of) more cross-browser and
reliable alternatives.
only to suggest a standards compliant method that works.
And where it works so does my proposal. But my alternative also works
where it doesn't (at least whenever adding options is possible in the
first place).
I didn't test my solution on IE for Mac, but
it did work in Mozilla for PC,
Mozilla supports all of the methods available (except the IE version of
the - add - method).
the OP's other required platform.

<snip>

The OP stated no platform requirements, only the results of testing. And
generally, when someone has gone to the trouble of testing with a
wide-ish range of browsers on multiple OSs there is a good chance that
there preference would be truly cross-browser (with the maximum
available active support).

Richard.
Jul 23 '05 #7

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

Similar topics

0
2646
by: Brian Greiwe | last post by:
I posted this in the datagrid forum but got no bites, so I thought I'd post it here as well for some help.... I've created a datagrid with 1 edititemtemplate column. When the user clicks...
5
2910
by: Tim | last post by:
I have block of code adding values to dropdown list under if (!Page.IsPostBack) { adding items } else { refreshing data }
5
11430
by: Kay | last post by:
Hello, I have two list boxes on my form, one initially displays with items and the other displays blank, by clicking a button, it is possible to move items from one box to another and vice a...
2
3034
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
2
3728
by: Dave | last post by:
Hi, I'm building a maintenance form for a table and some of the fields are textboxes (i.e name) and some should be dropdowns (i.e country of origin) When a user clicks 'Edit' in the...
6
3106
by: Juan Pedro Gonzalez | last post by:
I wanted to add a Combobox to a toolbar... Kind of the look you get on VisualStudio's toolbar. I've been able to find some VB 6 samples, but the placeholder option is no longer available for...
2
2747
by: John | last post by:
I have a listbox that is databound when my form loads. A user can then select and option using a drop down box. When the user selects an option the corresponding items in the listbox gets selected....
2
1581
by: Bruno Alexandre | last post by:
Hi guys, I'm using Javascript to dynamically add to a dropdown values like for ( i=0; i<tv.length; i++ ) { selectDDAvisCtrl.options = new Option(tv, tv); } and I use ...
1
7754
by: mitchman10 | last post by:
My Time table has TimeID,Employee,PayPeriod,ChargeCodeID,Hours My Chargecode table has ChargecodeID,c_Text I need an Editable datagrid that will show the TimeID,Employee,PayPeriod,C_Text in a...
0
7410
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...
1
7067
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
7505
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
5650
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,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
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...

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.