473,378 Members | 1,555 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,378 software developers and data experts.

Firefox, problems with dynamically filling a select-list

Hi there,
I'm trying to get this working with Firefox. Can you point out what's
wrong? No problems with IE, Firefox doens't fill selMonth...

TIA
the script... >>>


<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>

<script language="JavaScript">

function initDateSelect() {
for (x=1; x<32; x++) {
document.getElementById('selDay').options[x-1] = new Option(x,x); }
for (x=1; x<13; x++) {
document.getElementById('selMonth').options[x] = new Option(x,x); }
for (x=1920; x<2020; x++) {
document.getElementById('selYear').options[x-1920] = new Option(x,x); }
}

initDateSelect();
</script>
Jul 23 '05 #1
6 2965
The problem might be that you're starting at 1 instead of 0. I've never
tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {

I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );

----

Thought I'd also note that it's usually easier on the user to give them
a box to put the number in and then JS validate that number.

Sjaakie wrote:
Hi there,
I'm trying to get this working with Firefox. Can you point out what's wrong? No problems with IE, Firefox doens't fill selMonth...

TIA
>>> the script... >>>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>

<script language="JavaScript">

function initDateSelect() {
for (x=1; x<32; x++) {
document.getElementById('selDay').options[x-1] = new Option(x,x); }
for (x=1; x<13; x++) {
document.getElementById('selMonth').options[x] = new Option(x,x); }
for (x=1920; x<2020; x++) {
document.getElementById('selYear').options[x-1920] = new Option(x,x);

} }

initDateSelect();
</script>


Jul 23 '05 #2
Random wrote:
The problem might be that you're starting at 1 instead of 0. I've never
tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {
No effect...

I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );


I tried
document.getElementById("selDay").add( new Option( "test","test" ) );
no effect at all.

Firefox seems to act very differently compared to IE.
Jul 23 '05 #3
for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

Worked fine for me. FF apparently doesn't like making sparse arrays out
of .options lists, and I don't blame it.

Start at 0 for indices in .options.
My mistake on .add(), by the way. That's IE-only, so couldn't possibly
have fixed your problem.
Literally, here is the code I used:
<html><body><form>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>
<script language="JavaScript">

function initDateSelect( ) {
for( x = 1; x < 32; x++ )
document.getElementById( 'selDay' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1920; x < 2020; x++ )
document.getElementById( 'selYear' ).options[ x - 1920 ] =
new Option( x, x );

}
initDateSelect();

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

Sjaakie wrote:
Random wrote:
The problem might be that you're starting at 1 instead of 0. I've never tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {


No effect...

I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );


I tried
document.getElementById("selDay").add( new Option( "test","test" ) );
no effect at all.

Firefox seems to act very differently compared to IE.


Jul 23 '05 #4
This indeed did the trick, FF doesn't allow non-0-based option lists.
Thanks for your help.
Random wrote:
for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

Worked fine for me. FF apparently doesn't like making sparse arrays out
of .options lists, and I don't blame it.

Start at 0 for indices in .options.
My mistake on .add(), by the way. That's IE-only, so couldn't possibly
have fixed your problem.
Literally, here is the code I used:
<html><body><form>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>
<script language="JavaScript">

function initDateSelect( ) {
for( x = 1; x < 32; x++ )
document.getElementById( 'selDay' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1; x < 13; x++ )
document.getElementById( 'selMonth' ).options[ x - 1 ] =
new Option( x, x );

for( x = 1920; x < 2020; x++ )
document.getElementById( 'selYear' ).options[ x - 1920 ] =
new Option( x, x );

}
initDateSelect();

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

Sjaakie wrote:
Random wrote:
The problem might be that you're starting at 1 instead of 0. I've
never
tried making .options a sparse array, so not sure how well it would
handle that.

Try
for (x=0; x<12; x++) {


No effect...
I generally prefer something more like:

mySelectElement.add( new Option( strText, strValue ) );


I tried
document.getElementById("selDay").add( new Option( "test","test" ) );
no effect at all.

Firefox seems to act very differently compared to IE.

Jul 23 '05 #5
On 23/05/2005 12:15, Random wrote:

[snip]
My mistake on .add(), by the way. That's IE-only, so couldn't possibly
have fixed your problem.
No it isn't. The add method is defined by the W3C, but Microsoft have an
entirely incompatible implementation. The only way to use it reliably[1]
is to use a try/catch statement, but that will cause problems if a user
agent implements the add method, but not an exception mechanism.

[snip]
<select [...] style="width:50;">
With CSS, all non-zero length values must be accompanied by a unit
specifier.

[snip]
<script language="JavaScript">
The language attribute is deprecated. Use the (required) type attribute
instead:

<script type="text/javascript">
function initDateSelect( ) {
for( x = 1; x < 32; x++ )


If a variable has no business being global, ensure you declare it local.

function initDateSelect() {
var x;

/* ... */
}

[snip]

Mike
[1] I've experimented with other way that infers the expected
arguments based on the length property of the method, but
that may not be reliable.
Please don't use tabs when posting code. Use spaces - preferably two -
when indenting instead.

You've been instructed on how to post properly though Google Groups.
Please do so.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
Sjaakie wrote:
Hi there,
I'm trying to get this working with Firefox. Can you point out what's
wrong? No problems with IE, Firefox doens't fill selMonth...

TIA
>>> the script... >>>

<select name="selDay" id="selDay" style="width:50;" ></select> -
<select name="selMonth" id="selMonth" style="width:50;"></select> -
<select name="selYear" id="selYear" style="width:75;"></select>

<script language="JavaScript">

function initDateSelect() {
for (x=1; x<32; x++) {
document.getElementById('selDay').options[x-1] = new Option(x,x); }


Repeatedly using getElementById is likely slow, use a local variable
to hold a reference to the options and add to that.
for (x=1; x<13; x++) {
document.getElementById('selMonth').options[x] = new Option(x,x); }
Your first month option will be index 1, that seems to cause Firefox
to not display the options:

document.getElementById('selMonth').options[x-1] = new Option(x,x);

for (x=1920; x<2020; x++) {
document.getElementById('selYear').options[x-1920] = new Option(x,x); }
}

initDateSelect();
</script>


Here's another version that may run a bit faster and avoids
getElementById by using the forms collection (and therefore should
have wider browser support):

<form name="dForm" action="">
<select name="selDay" style="width: 3em;"></select> -
<select name="selMonth" style="width: 3em;"></select> -
<select name="selYear" style="width: 5em;"></select>
</form>
<script type="text/javascript">

var x = 0;
var opt = document.dForm.selDay.options;
while ( x++ < 31 ) { opt[x-1] = new Option(x,x); }

x = 0;
opt = document.dForm.selMonth.options;
while ( x++ < 12 ) { opt[x-1] = new Option(x,x); }

x = 0;
opt = document.dForm.selYear.options;
while ( x++ < 100 ) { opt[x-1] = new Option(x+1919,x+1919); }

</script>
Using 'em' as the unit for width means the size of the options will
scale to whatever font size the user's browser is set to. Using
script-generated form elements may cause accessibility issues for
some users and those without JavaScript will not see any options.
--
Rob
Jul 23 '05 #7

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

Similar topics

2
by: Jon | last post by:
Hi all, I am trying to dynamically fill an array with any unique values from a field (objRS.Fields(5)) in my database with the following code below. However it errors with "The array is fixed...
1
by: J. Muenchbourg | last post by:
I have 3 select pulldown boxes where a user will select a year (either 2002 or 2001), a week (one thru 52), and a database (short data or long data). But if a user selects a week that is less than...
11
by: lkrubner | last post by:
I'm facing an extremely irksome bug in FireFox. This select tag is not rendering quite right. When I click on it to choose a color, it shows a scroll tab, but the scroll only goes down to the 43...
3
by: crjunk | last post by:
I have a 3 table in my DataSet that I'm filling with data. After I've filled these 3 tables, I'm then trying to run a query that will fill a 4th table in the DataSet with data from the three...
2
by: tshad | last post by:
I have a couple of stored procedures that are being called from my Page_Load event. They worked before, but now they are not working. I am getting the error: Specified argument was out of...
5
by: BJ | last post by:
Hi, I add label and text box fields dynamically in code using C# and ASP.NET. I set the width of the label using: Label label = new Label(); label.Width = 20; label.Text = "Test";...
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: Hoss | last post by:
Hello all- This is what im trying to achieve. At the top of my page there is some search functionality, through which you cause to be loaded a string representing an HTML page. Below this and...
2
by: cbjewelz | last post by:
Hey all. So I'm having problems with cross browser alignments. I'm looking at Safari and Mozilla Firefox. I develop in Safari and so it looks perfect there however in Firefox my vertical...
4
by: Patrick Nolan | last post by:
I am using javascript to manipulate optgroups in select elements. When the results are displayed in Firefox 2.0 there is an annoying blank line at the top of the multi-line select box. This...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.