Hi there. I'm trying to figure out how to make this JS work with nested divs. Right now it looks for TagName 'div'. I have multiple nested divs and that seems to mess things up pretty good. I'm not sure how to add more detail to this code, like look for specific ID's instead of a global div tag.....
Here's the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Toggle Div Display</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
var articleIndex = [];
var articleLinks = "";
var articles = "";
var prevArticle = "";
var prevClicked = "";
var inactiveColor = "#add8e6";
var activeColor = "#ff69b4";
function toggle(currArticle){
if (prevArticle !== "")
{
articles[prevArticle].className = "toggleHide";
}
for (i=0; i<articleLinks.length; i++)
{
if (articleLinks[i] == currArticle)
{
articles[i].className = "toggleShow";
prevArticle = i;
}
}
}
function init(){
articleLinks = document.getElementsByTagName('a');
articles = document.getElementById('articleContainer').getElementsByTagName('div');
for (i=0; i<articleLinks.length; i++)
{
if (articleLinks[i].className == "articleLink")
{
articleIndex[articleIndex.length] = articleLinks[i];
articleLinks[i].onclick = function()
{
if (prevClicked != "")
{
prevClicked.style.color = inactiveColor;
}
this.style.color = activeColor;
prevClicked = this;
toggle(this);
return false;
} ;
}
}
}
navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);
</script>
<style type="text/css">
body {margin-top: 50px;}
.articleLink {color: #add8e6; text-decoration: none; margin-left: 10px; margin-right: 10px;}
.toggleHide {display: none;}
.toggleShow {display: block; font-family: arial; font-size: 12pt; background-color: #f0fff0; width: 150px;
height: 100px; padding: 3px;}
</style>
</head>
<body>
<a href="#" class="articleLink"> Show Article 1 </a>
<a href="#" class="articleLink"> Show Article 2 </a>
<a href="#" class="articleLink"> Show Article 3 </a>
<br><br>
<div id="articleContainer">
<div class="toggleHide"> 1st </div>
<div class="toggleHide">2nd
<div>nested div is a problem, delete this div and it works....but I have multiple nested divs.</div>
</div>
<div class="toggleHide"> This is article 3 </div>
</div>
</body>
</html>