Link to home
Start Free TrialLog in
Avatar of sitchey
sitchey

asked on

Help with HTML drop down menus

I created a website with a rollover naviagtion using html and javascript.  When you roll over the button you get a drop down menu of 4-8 items.  But i am now looking to add in a sub menu to a few of the drop down items.  Here is the code I have for 4 drop down items

##################################

      </TBODY></TABLE></DIV>
<DIV id=Menucf682a3
style="Z-INDEX: 9997; VISIBILITY: hidden; POSITION: absolute; left:14px; top:112px">
<TABLE cellSpacing=1 cellPadding=0 width=186 bgColor=#ffffff border=0>
  <TBODY>
  <TR>
    <TD class=laynav bgColor=#df6920><A onfocus=blurLink(this);
      href="#">link1</A></TD></TR>
               <TR>          
    <TD class=laynav bgColor=#df6920><A onfocus=blurLink(this);
      href="#">link2</A></TD></TR>
  <TR>
    <TD class=laynav bgColor=#df6920><A onfocus=blurLink(this);
      href="#"link3</A></TD></TR>
  <TR>
    <TD class=laynav bgColor=#df6920><A onfocus=blurLink(this);
      href="#">link4</A></TD></TR>
  <TR>
    <TD class=laynav bgColor=#df6920><A onfocus=blurLink(this);
      href="#">link5</A></TD></TR>
</TBODY></TABLE></DIV>

#################################################

Is there a way were i can add in a few submenus to "link1" & "Link2".

Any help would be really appreciated.
ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sitchey
sitchey

ASKER

I dont want to completely change all the code in my pages as the website is already up and this would take ages.

I would just like to make some quick changes to the above code.  Is there not a way i could add in something like <submenu> and the link to a few of the links above?
Unfortunately, no there is no other way to add submenu to the links above. The submenus are not an integral part of Javascript or HTML.
you could just add another div element with the sub-nav items in the same TD wrapper and then onmouseover event of the link1, link2 it showes the sub nav items.  The same effect could be acheived using javascript DOM level coding to insert sub menu items based on the parent link (link1, link2).
Avatar of sitchey

ASKER

ok what way would i add the new sub menu in?

####################################

  <TR>
    <TD class=laynav bgColor=#df6920><A onfocus=blurLink(this);
      href="#">link1</A></TD></TR>
              <TR>    

<TR>
 <TD Class=sub-nav bgColor=#df6920><A onfocus=blurLink(this);
      href="#">link1a</A></TD></TR>
              <TR>    
###########################################
Avatar of sitchey

ASKER

how would i add in the new div element for the submenu to appear after each link?
Here is an example I created using JS to create the sub-menu. The sub-menu items are declaired in the first few lines of the mArray with a object name (from link value), display name, url value for the links.  It then uses a for loop to only display the links for the corresponding parent clicked.  You can make the sub-menu a list item or whatever other html element you wish.

One could also expand this to do a close of the sub-menu on a second click, but that all depends on your application.

Cheers

#########################################
# Insert in a script tag in your document heading area            #
# or create a single .js file to call into all html pages (rec)       #
#########################################
var mArray = new Array(
                        ["href","name","url"],
                        ["link1","Sub1 Sample 1","sample1"],
                        ["link1","Sub1 Sample 2","sample2"],
                        ["link1","Sub1 Sample 3","sample3"],
                        ["link2","Sub2 Sample 1","sample1"],
                        ["link2","Sub2 Sample 2","sample2"]
                                    );


function addMenu(obj,name){
      var newItems = "";
      cObj = obj.lastChild;
      for(x = 0; x < mArray.length; x++){
            if(mArray[x][0] == name){
                  newItems += "<a href='"+ mArray[x][2] +"'>"+ mArray[x][1] +"</a><br />";
            }
      }
      if(obj.lastChild.nodeName == "DIV"){
            obj.lastChild.innerHTML = newItems;
      } else {
            myNewMenu = document.createElement("div");
            myNewMenu.setAttribute("name", name);
            myNewMenu.innerHTML = newItems;
            obj.appendChild(myNewMenu);
      }
}
</script>
#########################################

#########################################
# updates to the onclick or onfocus events to display submenu #
#########################################
<TR>
    <TD class=laynav bgColor=#df6920><A onfocus="blurLink(this);" onclick="addMenu(this.parentNode,this.innerHTML); return false" href="#">link1</A></TD>
</TR>


<TR>
      <TD Class=sub-nav bgColor=#df6920><A onfocus="blurLink(this);addMenu(this.parentNode,this.innerHTML); return false" href="#">link2</A></TD>
</TR>
#########################################
sitchey, was the first to reply and give a solution