troubleshooting Question

Help modifying expanding menu using javascript

Avatar of shepherdIT
shepherdITFlag for United States of America asked on
JavaScriptCSS
11 Comments2 Solutions360 ViewsLast Modified:
Hi, This web page has an expanding/collapsing menu on the left side and the menu items have links that use onClick to call a javascript function to switch out the image on the right side of the page in the mainContent div.  The sublinks and expanding/collapsing work fine, but I want the top menu items such as 'Introduction' and 'Assistive Technology' to also change the mainContent image when clicked on.    When I try to add a similar link on these top level menu items, then the sub-menu items won't expand.  

--------------------------- Javascript --------------------

function changeImg(img,w,h) 
{
var ipath = "url(" + img + ")" ;
var hstring = h + 'px';
var wstring = w + 'px';

document.getElementById("mainContent").style.backgroundImage = ipath;
document.getElementById("mainContent").style.width = wstring;
document.getElementById("mainContent").style.height = hstring;
}


// JavaScript Document
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Travis Beckham :: http://www.squidfingers.com | http://www.podlob.com
version date: 06/02/03 :: If want to use this code, feel free to do so,
but please leave this message intact. (Travis Beckham) */

// Node Functions

if(!window.Node){
  var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
}

function checkNode(node, filter){
  return (filter == null || node.nodeType == Node[filter] || node.nodeName.toUpperCase() == filter.toUpperCase());
}

function getChildren(node, filter){
  var result = new Array();
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    if(checkNode(children[i], filter)) result[result.length] = children[i];
  }
  return result;
}

function getChildrenByElement(node){
  return getChildren(node, "ELEMENT_NODE");
}

function getFirstChild(node, filter){
  var child;
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    child = children[i];
    if(checkNode(child, filter)) return child;
  }
  return null;
}

function getFirstChildByText(node){
  return getFirstChild(node, "TEXT_NODE");
}

function getNextSibling(node, filter){
  for(var sibling = node.nextSibling; sibling != null; sibling = sibling.nextSibling){
    if(checkNode(sibling, filter)) return sibling;
  }
  return null;
}
function getNextSiblingByElement(node){
        return getNextSibling(node, "ELEMENT_NODE");
}

// Menu Functions & Properties

var activeMenu = null;

function showMenu() {
  if(activeMenu){
    activeMenu.className = "";
    getNextSiblingByElement(activeMenu).style.display = "none";
  }
  if(this == activeMenu){
    activeMenu = null;
  } else {
    this.className = "active";
    getNextSiblingByElement(this).style.display = "block";
    activeMenu = this;
  }
  return false;
}

function initMenu(){
  var menus, menu, text, a, i;
  menus = getChildrenByElement(document.getElementById("menu"));
  for(i = 0; i < menus.length; i++){
    menu = menus[i];
    text = getFirstChildByText(menu);
    a = document.createElement("a");
    menu.replaceChild(a, text);
    a.appendChild(text);
    a.href = "#";
    a.onclick = showMenu;
    a.onfocus = function(){this.blur()};
  }
}

if(document.createElement) window.onload = initMenu;



-------- end of Javascript -----

---------Beginning of CSS ------

@charset "utf-8";
body  {
	font:Arial, Helvetica, sans-serif;
	font-size: 18px;
	background: #CCC;
	margin: 0; 
	padding: 0;
	text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in 

the #container selector */
	color: #000000;
}
.twoColFixLt #container { 	
	width: 1100px;
	background: #FFFFFF;
	margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
	text-align: left; /* this overrides the text-align: center on the body element. */
}
.twoColFixLt #sidebar1 {
	float: left; /* since this element is floated, a width must be given */
	width: 300px;	
	padding: 15px 10px 15px 20px;
	background: #39C;
}
.twoColFixLt #mainContent { 	
	margin: 0 0 0 350px;
	padding: 0 20px 20px; 
	padding-bottom: 10px;
	position:relative;	
	background-repeat:no-repeat;
	background-position: left top;
	background-image:url(../DME/images/homepage.jpg);
	width: 684px;
	height: 684px;
} 
.fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should 

be next to on the page. */
	float: right;
	margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page */
	float: left;
	margin-right: 8px;
}
.clearfloat { /* this class should be placed on a div or break element and should be the final element before the close of a 

container that should fully contain a float */
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}
ul#menu {
  width: 250px;
  list-style-type: none;
  border:none;
  margin: 0;
  padding: 0;
}

ul#menu ol {
  display: none;
  text-align: left;
  list-style-type: none;
  margin: 0;
  padding-left: 12px;
  padding-top: 3px;
  padding-bottom: 3px;
  padding-right: 0px;
}

ul#menu li, 
  ul#menu a {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
  font-weight:bold;
  color:#000;
}

ul#menu li {
  line-height: 30px;
}

ul#menu ol li {
  border-bottom: none;
}

ul#menu ol li:before {
  content: ">";
}

ul#menu a {
  text-decoration: none;
  outline: none;
}

ul#menu a:hover {
  color: #FFF;
}

ul#menu a.active {
  color:#FFF;
}


------------ end of CSS ------------------------

------------ beginning of HTML -------------------

                         
<body class="twoColFixLt">

<div id="container">

  <div id="header">
	<h1 align="center">DME Guide</h1>
  </div>
  <div id="sidebar1">
    
<ul id="menu">
  <li> Introduction
    <ol>            
      <li> <a href="#" onclick="changeImg('images/introtext.jpg','684','684')">Overview</a>  </li>            	   
      <li> <a href="#" onclick="changeImg('images/committee.jpg','684','792')">Rehab Standards Committee</a></li>

    </ol>
  </li>
  <li>Assistive Technology
    <ol>
      <li> <a href="#">C1-3</a></li>
      <li> <a href="#">C4</a></li>
      <li> <a href="#">C5</a></li>
      <li> <a href="#">C6-T1</a></li>
    </ol>
  </li>
  <li>Bathing and Toileting
    <ol>
      <li> <a href="#">C1-4</a></li>
      <li> <a href="#">C5-6</a></li>
      <li> <a href="#">C7-T1</a></li>
      <li> <a href="#">T2-T12</a></li>
      <li> <a href="#">L1-S5</a></li>
    </ol>
  </li>

</ul>    
    
  <!-- end #sidebar1 --> </div>
  
  <div id="mainContent">
  </div>
	
  <br class="clearfloat" />

<!-- end #container --></div>

</body>
</html>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros