Hide Divs

S

shapper

Hello,

I created a function to show a div given its id and hide the other
divs (defined in a div):

function show(id) {
//var e = document.getElementsByTagName("div");
var e = {"home", "contact", "products", "photos"};
for (var i = 0; i < e.length; i++) {
e.style.display = 'none';
if (e.id == id)
e.style.display = 'block';
}
}

This is not working. What am I doing wrong?

It works if I use:
var e = document.getElementsByTagName("div");

But I wan to hide the divs which id's are in the list and show the one
in that list that has the given id.

Thanks,
Miguel
 
T

Thomas 'PointedEars' Lahn

Dan said:
shapper said:
I created a function to show a div given its id and hide the other
divs (defined in a div):

function show(id) {
//var e = document.getElementsByTagName("div");
var e = {"home", "contact", "products", "photos"};
for (var i = 0; i < e.length; i++) {
e.style.display = 'none';
if (e.id == id)
e.style.display = 'block';
}
}

This is not working. What am I doing wrong?

It works if I use:
var e = document.getElementsByTagName("div");


Your variable e is simply an array of strings, not elements.


There is no variable, there is a syntax error. In contrast to Java, Array
initializers are delimited by `[' and `]' in ECMAScript implementations.
`{' and `}' delimit Object initializers and BlockStatements instead.

Since an expression is expected right-hand side here, an Object initializer
would be expected. However, the syntax for that is {property: value, ...}
or {"property": value, ...} instead.
Why did you comment out the 'getElementsByTagName' line?

I don't know either. Those two lines are not the least equivalent. The
former would return a reference to a NodeList host object, the latter to a
native Array object if it was properly delimited.


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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top