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