newbie document.write with DIV tag problem = ID's are not defined

A

Andrew Crook

I had a web page with many DIV tags with the same ID and I had some
JavaScript that would loop and manipulate them. However, when I use
document.write in JavaScript to write this same code, JavaScript seems to
think that these ID are not defined


Any ideas on how to resolve this

Andrew
 
L

Lee

Andrew Crook said:
I had a web page with many DIV tags with the same ID and I had some
JavaScript that would loop and manipulate them. However, when I use
document.write in JavaScript to write this same code, JavaScript seems to
think that these ID are not defined


Any ideas on how to resolve this

No two objects in any page should ever have the same ID.
Fix your design.
 
L

Lasse Reichstein Nielsen

Andrew Crook said:
I had a web page with many DIV tags with the same ID

That's an error. IDs must be unique in an HTML document.
and I had some JavaScript that would loop and manipulate
them. However, when I use document.write in JavaScript to write this
same code, JavaScript seems to think that these ID are not defined
Ok.

Any ideas on how to resolve this

Not without seeing the code. I can think of several errors that can
cause the problem.

/L
 
A

Andrew Crook

That's an error. IDs must be unique in an HTML document

I had several of the following defined

<div id="menu" class="menu0">

the classes was different because CCS held positiion info, but ID's were the
same.

i could then use the following javascript

for (menuIdx=0;menuIdx<menu.length;menuIdx++)
{
menu[menuIdx].style.visibility = "hidden"
}
}


menu.length seems to work fine
menu was not defined in javascript and this worked perfectly, until i tried
to define the DIV's via document.write (now menu is not defined).

Andrew
 
L

Lasse Reichstein Nielsen

Andrew Crook said:
I had several of the following defined

<div id="menu" class="menu0">

It is incorrect HTML (i.e., your document is not HTML, it just looks
vaguely like it).
the classes was different because CCS held positiion info, but ID's were the
same.

i could then use the following javascript

for (menuIdx=0;menuIdx<menu.length;menuIdx++)
{
menu[menuIdx].style.visibility = "hidden"

More than one element having the same ID is incorrect HTML. IE allows it
anyway, but that doesn't make it right, nor does it make it recommendable,
since other browsers might act differently.

Using the name of an element as a global variable is again an IE specific
behavior. Other browsers will fail on that line, even if only one element
had id="menu".

The correct way is:
document.getElementById("menu").style.visiblity = "hidden";
and only one element should be fetched (it's getElement, not getElement*s*)
menu.length seems to work fine

Seems? How? Work? how? Fine? (you get the point: Be more specific!)
menu was not defined in javascript and this worked perfectly, until i tried
to define the DIV's via document.write (now menu is not defined).

So you didn't show us the part of the code you changed when the problem
started. :)
I bet the problem is in that code.

/L
 
M

Michael Winter

Andrew Crook wrote on 25 Nov 2003:

menu.length seems to work fine
menu was not defined in javascript and this worked perfectly,
until i tried to define the DIV's via document.write (now menu
is not defined).

<snip>

Just because it works, doesn't make it right.


Mike

And don't top-post.
 
E

Evertjan.

Michael Winter wrote on 25 nov 2003 in comp.lang.javascript:
Just because it works, doesn't make it right.

Just because some code does not work, it isn't necessarily wrong.

;-}
 
F

Fabian

Evertjan. hu kiteb:
Michael Winter wrote on 25 nov 2003 in comp.lang.javascript:

Just because some code does not work, it isn't necessarily wrong.

Surely, the only pragmatic standard is, does it work?
 
L

Lasse Reichstein Nielsen

Fabian said:
Surely, the only pragmatic standard is, does it work?

Yes. It's the definition of "work" that is the real problem.

"Works in my browser on my computer" is not a good definition :)

/L
 
E

Eric Bohlman

Evertjan. hu kiteb:


Surely, the only pragmatic standard is, does it work?

The problem with applying that standard to cases where a specification has
been violated is that very often "it works" means "it works in the cases I
tried." The problem is that the cases you've tried are always going to be
a small subset of the cases your code is going to encounter in the Real
World. It's all too easy to create something that works correctly, but
only under ideal conditions.

Simple case in point: one early version of Netscape would recover from
missing closing quotes in HTML attribute values. This lead a lot of
designers to get sloppy and write things like <a href="someurl><img
src="mypic.gif></a>. It worked correctly for them. But the next version
of Netscape to come out used different parsing code. And all of a sudden
big chunks of many pages went missing. The newer version didn't see an
image nested inside an anchor in the above example; it saw an empty anchor
with an href attribute set to "someurl<>img src=" and an empty "mypic.gif"
attribute.

Designers who were careful to properly close quotes didn't get complaints
from their clients that stuff was missing from their pages, even though the
designers who did wind up getting the complaints would initially have
called the former group "anal" rather than "careful." In fact the
"relaxed" designers were actually relying on the existence of a *bug* in
the early version of Netscape. Bugs tend to get fixed eventually.
 
A

Andrew Crook

thanxs i will go a way and get the ID in the html right first :)

Andi

Lasse Reichstein Nielsen said:
Andrew Crook said:
I had several of the following defined

<div id="menu" class="menu0">

It is incorrect HTML (i.e., your document is not HTML, it just looks
vaguely like it).
the classes was different because CCS held positiion info, but ID's were the
same.

i could then use the following javascript

for (menuIdx=0;menuIdx<menu.length;menuIdx++)
{
menu[menuIdx].style.visibility = "hidden"

More than one element having the same ID is incorrect HTML. IE allows it
anyway, but that doesn't make it right, nor does it make it recommendable,
since other browsers might act differently.

Using the name of an element as a global variable is again an IE specific
behavior. Other browsers will fail on that line, even if only one element
had id="menu".

The correct way is:
document.getElementById("menu").style.visiblity = "hidden";
and only one element should be fetched (it's getElement, not getElement*s*)
menu.length seems to work fine

Seems? How? Work? how? Fine? (you get the point: Be more specific!)
menu was not defined in javascript and this worked perfectly, until i tried
to define the DIV's via document.write (now menu is not defined).

So you didn't show us the part of the code you changed when the problem
started. :)
I bet the problem is in that code.

/L
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top