Looping for Element Ids

R

Rick W.

OK everyone, I know this is not orthodox but I have a situation where
there could be more than one drop-down option element with the same ID
in my document.
I am setting the value of that programatically in javascript using the
"getElementbyID" call.

However It only finds the first occurance and sets it. I need to be
able to set all of them,
there are some cases where it may show up 2 or 3 times in my document.

I have something like this.

function setallmatrix(matrixid,FC_option)
{

matrixoptions = document.getElementById(matrixid)

for(var i=0;i<matrixoptions.length;i++)

{

if(elements[x].id == matrixid)
{
matrixoptions.options[FC_option].selected = true;

}
}

As you can see I am trying to set a dropdown option control.
setting the first one like this works fine:
document.getElementById(matrixsizeop).options[FC_option].selected =
true;
but I have more than one on rare occasion.

Could someone please help,
I've tried all I know, and running out of time and options.

Rick
 
S

SAM

Le 11/24/08 9:38 PM, Rick W. a écrit :
OK everyone, I know this is not orthodox but I have a situation where
there could be more than one drop-down option element with the same ID
in my document.

It is formally forbidden !
I am setting the value of that programatically in javascript using the
"getElementbyID" call.

Usually we loop on options about their values or texts ...
However It only finds the first occurance and sets it. I need to be
able to set all of them,
there are some cases where it may show up 2 or 3 times in my document.

I have something like this.

Can't you give a 'name' to your options ?

function setallmatrix(matrixid,FC_option) {
// all seletcs named 'matrixid' :
matrixoptions = document.getElementsByName(matrixid);
for(var i=0, n = matrixoptions.length; i<n; i++)
matrixoptions.options[FC_option].selected = true;
}

if not, then :

function setallmatrix(matrixid,FC_option) {
// all seletcs of the page :
matrixoptions = document.getElementsByTagName('SELECT');
for(var i=0, n = matrixoptions.length; i<n; i++)
if(matrixoptions.id == matrixid)
matrixoptions.options[FC_option].selected = true;
}
 
T

Thomas 'PointedEars' Lahn

Rick said:
OK everyone, I know this is not orthodox but I have a situation where
there could be more than one drop-down option element with the same ID
in my document.

You MUST design that out of the Web application. Understand `MUST' here as
an absolute requirement as defined in RFC2119, since duplicate IDs in an
SGML-based markup document are *forbidden* (isn't that obvious by the term
already?)
I am setting the value of that programatically in javascript using the
"getElementbyID" call.

The call would have to be `....getElementById(...)', as ECMAScript
implementations are case-sensitive. And there is no "javascript"; the
feature we are talking about would not even be part of any such language.
However It only finds the first occurance and sets it.

Works as designed.
I need to be able to set all of them, there are some cases where it may
show up 2 or 3 times in my document.

Then I'm afraid your document is severely borken.

I have something like this.

[source code pretty-printed]
function setallmatrix(matrixid, FC_option)
{
matrixoptions = document.getElementById(matrixid)

for (var i = 0; i < matrixoptions.length; i++)

Document::getElementById() does not return a NodeList or a HTMLCollection,
so this could only work if you retrieved one element object that had a
`length' property.

You want to use names instead of IDs and an implementation of
HTMLDocument::getElementsByName() instead of an implementation of
Document::getElementById(). Note the plural in the former identifier
and the lack of it in the latter.

You also

- seem to have missed to declare `matrixoptions'
which is error-prone;

- did not end the assignment statement with a semicolon
(which is optional, but strongly recommended);

- retrieved the `length' property repeatedly when
there is no apparent need for that.


HTH

PointedEars
 
S

SAM

Le 11/24/08 9:58 PM, SAM a écrit :
if not, then :

function setallmatrix(matrixid,FC_option) {
// all seletcs of the page :
matrixoptions = document.getElementsByTagName('SELECT');
for(var i=0, n = matrixoptions.length; i<n; i++)
if(matrixoptions.id == matrixid)
matrixoptions.options[FC_option].selected = true;
}




function setallmatrix(matrixid,FC_option) {
// all seletcs of the page :
matrixoptions = document.getElementsByTagName('SELECT');
for(var i=0, n = matrixoptions.length; i<n; i++)
if(matrixoptions.id == matrixid)
matrixoptions.options[FC_option].selected = true;
}
 
T

The Natural Philosopher

SAM said:
Le 11/24/08 9:38 PM, Rick W. a écrit :

It is formally forbidden !


Usually we loop on options about their values or texts ...


Can't you give a 'name' to your options ?

Only form elements are allowed names.

A better answer is to take some random number on the back of the id, and
then get all the (divs?) and pattern match for ones that match the first
few characters, and sort em out from that.

In short, however you do it, they have to have unique IDS and no names.
 
D

David Mark

Le 11/25/08 1:50 AM, The Natural Philosopher a écrit :



and .... options aren't form elements ?

Not really. I think he literally meant FORM elements. Doesn't really
matter as the statement is wrong either way. Best to ignore anything
posted by the philosopher.
 
R

Rick W.

Thank You SAM
That worked Beautifully !!!

And thank you everyone for chiming in.
I learned a lot. (as i usually do from this group).
Don't know what I'd do without it!

Rick
 
S

SAM

Le 11/25/08 10:11 AM, SAM a écrit :
Le 11/25/08 1:50 AM, The Natural Philosopher a écrit :

and .... options aren't form elements ?


I wanted to say : 'selects' and not 'options'
as, finaly, it is question of selects
 
T

The Natural Philosopher

SAM said:
Le 11/25/08 1:50 AM, The Natural Philosopher a écrit :

and .... options aren't form elements ?

Nope. They are a type of derivative ;-)

My excuse is

1/. The word is generic, and
2/. I am busy replacing my <option> code with flyout javascript menus.
And assumed he was doing the same.
 
S

SAM

Le 11/25/08 10:38 AM, Rick W. a écrit :
Thank You SAM
That worked Beautifully !!!

I'm always surprised when somebody try to use DOM ans some functions
according with it while there is something else very simple to achieve
the job in old javascript (versions pre 1.3)
 
R

Rick W.

opps.
OK this is working in Firefox only Not IE?
any Ideas why?

function setallmatrix(matrixid,FC_option)
{
// all selects of the page :
matrixoptions = document.getElementsByTagName('SELECT');
for(var i=0, n = matrixoptions.length; i<n; i++)
{
if(matrixoptions.id == matrixid)
{
matrixoptions.options[FC_option].selected = true;
}
}
}
 
S

SAM

Le 11/25/08 9:23 PM, Rick W. a écrit :
opps.
OK this is working in Firefox only Not IE?

there is no reason for that doesn't work
any Ideas why?

without the context : no
function setallmatrix(matrixid,FC_option)
{
// all selects of the page :
matrixoptions = document.getElementsByTagName('SELECT');
for(var i=0, n = matrixoptions.length; i<n; i++)
{
if(matrixoptions.id == matrixid)
{
matrixoptions.options[FC_option].selected = true;
}
}
}


Works fine for me,
demo :
<http://cjoint.com/?lzw7O3vyyk>
 
S

SAM

Le 11/25/08 11:04 PM, SAM a écrit :
Le 11/25/08 9:23 PM, Rick W. a écrit :

there is no reason for that doesn't work

Ooops !

You didn't take the corrected version !
(posted at: Tue, 25 Nov 2008 00:14:15 +0100)

and ... with Fx it would have to not more work than with IE

without the context : no

if(matrixoptions.id == matrixid)
{
matrixoptions.options[FC_option].selected = true;
}
}
}
 
T

Thomas 'PointedEars' Lahn

Rick said:
OK this is working in Firefox only Not IE?

"Does not work" is a useless error description. [psf 4.11]

any Ideas why?

function setallmatrix(matrixid,FC_option)
{
// all selects of the page :
matrixoptions = document.getElementsByTagName('SELECT'); ^
for(var i=0, n = matrixoptions.length; i<n; i++)
{
if(matrixoptions.id == matrixid) ^^
{
matrixoptions.options[FC_option].selected = true;
}
}
}


You have not taken heed of *all* the advice I gave, and you have not posted
how you call the method, which is vital for an analysis. That said, you
should employ a debugger before you post on that matter here again.

<http://jibbering.com/faq/#debugging>

And you should add your last name while you are at it, Rick W. #4711.


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
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top