I have a script here, a real easy one. When you click a link, instead of anchoring the user to the section, it hides each section and based off the link you select the corresponding text will appear in a DIV.
Except right now, you click on link one, and the right text comes up. Then you click on link 2 and nothing happens, then you click on link three and the text for link 2 shows up.
I'm assuming it's something with my for loop, so then I tried to do iterate every other object, but then the script froze my browser.... So I don't know I'm so confused how such a basic script is giving me a really odd problem.
Here they are:
HTML Code:
<div id="links"><a href="#1">Link 1</a><a href="#2">Link 2</a><a href="#3">Link 3</a><a href="#4">Link 4</a></div><div id="information"><div id="1" name="1">This is the description for the first div element</div><div id="2" name="2">This is the description for the second div element</div><div id="3" name="3">This is the description for the third div element</div><div id="4" name="4">This is the description for the fourth element</div></div>
Firefox's Firebug showed me the issue. The problem is that there are text nodes between each of your four DIV's -- thus you can't retrieve a node by relative number. So, change your four HREF's and four DIV ID's because ID's are supposed to start with a letter. Then, change this statement:
text = elems.childNodes[div[1]].firstChild.nodeValue;
To this:
text = elems.getElementById(div.substr(1)).firstChild.nodeValue;
Hard to follow directions, is it? This is what the HTML is supposed to look like. Note that I made the links match the div's and removed the name attribute from the div's:
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Object Replacement</title><script src="test_files/main.js" type="text/javascript"></script></head><body><div id="links"><a href="#n1">Link 1</a><a href="#n2">Link 2</a><a href="#n3">Link 3</a><a href="#n4">Link 4</a></div><div id="information" style="visibility: hidden;"><div id="n1">This is the description for the first div element</div><div id="n2">This is the description for the second div element</div><div id="n3">This is the description for the third div element</div><div id="n4">This is the description for the fourth element</div></div><div id="content" style="overflow: visible;">This is the Text</div></body></html>
Bookmarks