By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,237 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,237 IT Pros & Developers. It's quick & easy.

Hide Divs

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
shapper wrote:
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?
Jun 27 '08 #2

P: n/a
Dan Rumney wrote:
shapper wrote:
>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.
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
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.