Connecting Tech Pros Worldwide Help | Site Map

Hide Divs

shapper
Guest
 
Posts: n/a
#1: Jun 27 '08
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[i].style.display = 'none';
if (e[i].id == id)
e[i].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
Dan Rumney
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Hide Divs


shapper wrote:
Quote:
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[i].style.display = 'none';
if (e[i].id == id)
e[i].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.

Why did you comment out the 'getElementsByTagName' line?
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Hide Divs


Dan Rumney wrote:
Quote:
shapper wrote:
Quote:
>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[i].style.display = 'none';
> if (e[i].id == id)
> e[i].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.
Quote:
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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Closed Thread