Looping to populate selections for IE & Firefox

R

Rick W.

<script>

function filldims()
{
var objSelect = document.getElementById("id_intwidth");
objSelect.options.length = 0;

for(var i = 20; i < 31; i++)
{
var objOption = document.createElement("option");
objOption.text = i;
objOption.value = i;

//the following line WORKS IN FIREFOX ONLY
objSelect.add(objOption,
objSelect.options[objSelect.options.length]);


};
}

</script>
 
T

Thomas 'PointedEars' Lahn

Rick said:
//the following line WORKS IN FIREFOX ONLY
objSelect.add(objOption,
objSelect.options[objSelect.options.length]);

That is incorrect. It works in all user agents that have the object
referred to by `objSelect' implement the HTMLSelectElement interface
of W3C DOM Level 2 HTML. However, it should be noted that passing
`null' for the second argument suffices here:

<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-14493106>


PointedEars
 
R

Rick W.

I added the 'null' but it still didn't work.
but if you look closer I AM passing two parameters already.

In IE, I do get a list of blank items.
another words, the list is now 10 items long, but the list has blank
values in it.

Rick
 
T

Thomas 'PointedEars' Lahn

Rick said:
I added the 'null' but it still didn't work.

Nobody said anything about *adding* `null'. I suggested you should
*replace* the second argument with `null' because it is more efficient (and
less error-prone) than retrieving an (obviously) non-existing reference
through a property access.
but if you look closer I AM passing two parameters already.

If you look closer, nobody debated that in the first place.
In IE, I do get a list of blank items. another words, the list is now 10
items long, but the list has blank values in it.

Nobody debated that IE does not implement the HTMLSelectElement interface
for its Select objects either.

Since apparently you don't see the problem, I would like to point out to you
that your posting completely lacks a question.

Please read the FAQ carefully: <http://jibbering.com/faq/>

And it wouldn't hurt to post under your full name, Rick W. #4711.


PointedEars
 
S

SAM

Le 11/3/08 8:53 PM, Rick W. a écrit :
<script>

function filldims()
{
var objSelect = document.getElementById("id_intwidth");
objSelect.options.length = 0;

for(var i = 20; i < 31; i++)
{
var objOption = document.createElement("option");
objOption.text = i;
objOption.value = i;

//the following line WORKS IN FIREFOX ONLY
objSelect.add(objOption,
objSelect.options[objSelect.options.length]);

objSelect.appendChild(objOption); // would have to work


function filldims()
{
var objSelect = document.getElementById("id_intwidth");
objSelect.options.length = 0;

for(var i = 20; i < 31; i++)
{
objSelect.options[objSelect.options.length] = new Option(i,i);
}
}
 
S

Stanimir Stamenkov

Mon, 3 Nov 2008 11:53:26 -0800 (PST), /Rick W./:
function filldims()
{
var objSelect = document.getElementById("id_intwidth");
objSelect.options.length = 0;

for(var i = 20; i < 31; i++)
{
var objOption = document.createElement("option");
objOption.text = i;
objOption.value = i;

//the following line WORKS IN FIREFOX ONLY
objSelect.add(objOption,
objSelect.options[objSelect.options.length]);

The line actually works in all standard-compliant [1] browsers, but
Internet Explorer (as already pointed out in another reply). Try
using just:

objSelect.add(objOption);

IE versions prior 8 expect an optional index [2] and not an object
reference as a second argument.

[1] http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-14493106
[2] http://msdn.microsoft.com/library/ms535921(en-us,VS.85).aspx
 
R

Richard Maher

Hi Rick,
//the following line WORKS IN FIREFOX ONLY

Might not be of any use to you, but I use this sort of thing: -

queList = document.queOptions.queList;
: : :
queList.length = zeros;
queList.options[queList.length] = new Option (msgQue, msgQue);
OR
selectRef = document.getJobs.jobList;
selectClone = selectRef.cloneNode(true);
: : :
selectRef.size = 1;
swapClone = selectClone.cloneNode(true);
selectRef.parentNode.replaceChild(swapClone, selectRef);
selectRef = document.getJobs.jobList;
: : :
selectRef.options[selectRef.options.length] = new Option (jobMsg,
jobMsg);

For complete example code please see: -
http://manson.vistech.net/t3$examples/demo_client_web.html

Username: TIER3_DEMO
Password: QUEUE

Enter "*" asterix for the Queue Name and then hit "Get Job Info"

All source can be found at: -
http://manson.vistech.net/t3$examples/

Cheers Richard Maher

Rick W. said:
<script>

function filldims()
{
var objSelect = document.getElementById("id_intwidth");
objSelect.options.length = 0;

for(var i = 20; i < 31; i++)
{
var objOption = document.createElement("option");
objOption.text = i;
objOption.value = i;

//the following line WORKS IN FIREFOX ONLY
objSelect.add(objOption,
objSelect.options[objSelect.options.length]);


};
}

</script>
 
T

Thomas 'PointedEars' Lahn

Stanimir said:
Mon, 3 Nov 2008 11:53:26 -0800 (PST), /Rick W./:
//the following line WORKS IN FIREFOX ONLY
objSelect.add(objOption,
objSelect.options[objSelect.options.length]);

The line actually works in all standard-compliant [1] browsers, but
Internet Explorer (as already pointed out in another reply). Try
using just:

objSelect.add(objOption);

Bad advice. The second argument of HTMLSelectElement::add() is mandatory;
with the call above you are risking an exception along the lines of "not
enough arguments".
IE versions prior 8 expect an optional index [2] and not an object
reference as a second argument.

So it would appear to be a viable approach, unless MSHTML < 8 throws
exceptions then, to pass `null' for the second argument, check the `value'
or `text' of the newly added item, and modify its `value' and `text'
properties when necessary.

However, there is also a well-documented proprietary, but apparently
cross-browser approach (originating from "DOM Level 0"):

var opts = sel.options;
opts[opts.length] =
new Option("text", "value, defaultSelected, selected);

whereas the second argument is optional, and `defaultSelected' and
`selected' are optional boolean values that have the meaning of their
identifiers.


PointedEars
 
R

Rick W.

Thank you SAM
Your solution appears to work fine in both browsers.

I'm going to go with it.
Hopefully there are no hidden issues with this approach that I
inevitably find out later.

Thanks again,
Rick
 
T

Thomas 'PointedEars' Lahn

Rick said:
Thank you SAM
Your solution appears to work fine in both browsers.

IE 6 and IE 7, I presume ...

To learn you have a lot, young apprentice.


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,794
Members
47,342
Latest member
eixataze

Latest Threads

Top