Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem keeping a value set

    its me again, and i'm still working on the same code.

    assume menu.length = 3;
    Code:
             for (menulen=0; menulen!=menu.length; menulen++) {
                 ..........
                 linkelement.onmouseover = function() { expand(menulen); };
                 ..........
                 }
    the problem i'm having now is that EVERY linkelement uses expand(3) instead of the number of the loop it was on. i don't even understand how it got to 3, seeing as how its supposed to stop at 2.

    here's my code yet again:

    Code:
    <script type="text/javascript">
    
    var idgroups = new Array();
    var menulist = document.createElement("ul");
    var submenulist;
    var menulist;
    var menuitem;
    var linkelement;
    var sublinkelement;
    var menuelement = document.getElementById("menu");
    var menuitemname;
    var submenuitemname;
    var menuitemvalue;
    
    var menu = new Array();
    var menuvars = new Array();
    var submenu = new Array();
    var subvars;
    
    var menu = ["One","Two","Three"];
    var menuvars = ["assassinate","your","parents"];
    var submenu = [new Array("HEH","heh","hEh"),new Array("Rofl","Lmao","LoL"),new Array("I","like","poop")];
    var subvars = [new Array("...","...","..."),new Array("...","...","..."),new Array("...","...","...")];
    
    var submenucontent = new Array();
    
    
    
    
    function buildmenu() {
             menuelement = document.getElementById("menu");
    
             for (menulen=0; menulen!=menu.length; menulen++) {
                 // here we create teh menu items
                 menuitem = document.createElement("li");
                 linkelement = document.createElement("a");
                 linkelement.setAttribute("href",menuvars[menulen]+"/index.phtml");
                 linkelement.setAttribute("id",menulen);
                 linkelement.onmouseover = function() { expand(menulen); };
                 menuitemname = document.createTextNode(menu[menulen]);
                 linkelement.appendChild(menuitemname);
                 menuitem.appendChild(linkelement);
                 var tempvar="";
                 // this is whurr we make teh submenu items
                 submenulist = document.createElement("ul");
                 for (submenulen=0; submenulen!=submenu[menulen].length; submenulen++) {
                     submenuitem = document.createElement("li");
                     sublinkelement = document.createElement("a");
                     sublinkelement.setAttribute("href",menuvars[menulen]+"/"+subvars[menulen][submenulen]+".phtml");
                     submenuitemname = document.createTextNode(submenu[menulen][submenulen]);
                     sublinkelement.appendChild(submenuitemname);
                     submenuitem.appendChild(sublinkelement);
                     submenulist.appendChild(submenuitem);
    
    
                     menuitem.appendChild(submenulist);
    
    
                     }
                 
                 // this is whurr we store teh submenu items.
    
                 tempvar = submenulist;
                 submenucontent.push(tempvar);
    
                 menulist.appendChild(menuitem);
                 }
              menuelement.appendChild(menulist);
              }
    
    function expand(menuID) {
             document.write(menuID);
             blah.appendChild(submenucontent[0]);
             }
    
    </script>
    <body onload="buildmenu()">
    <div id="menu">
    
    </div>
    <div id="gay">
    </div>
    </body>
    yet again i realize there are some things that shouldnt really be in there, but these do not affect the code i am referencing. anybody have any ideas?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The menulen variable is global because it has no var keyword when declared. So when a link is mouseovered, it gets the value of the global variable.

    for (var menulen=0; menulen<menu.length; menulen++)

    It's better to use the menulen<menu.length than menulen!=menu.length.
    Using your original conditional operator !=, there is a chance that you would have an endless loop if inside the loop, you accidentally set the iterator variable menulen to a value greater than the menu length.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the pointer, however that did not solve the problem. each menu item is still marked as 3.

    they should be marked individually as 1, 2, and 3.

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Well, I started going through your code. Cleaning it up, changing it to resemble some form of sense, but it's just not worth it when the way to do it properly is so easy.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Since you set menulen as the id of the link, use the id as the parameter to the expand function:

    linkelement.setAttribute("id",menulen);
    linkelement.onmouseover = function() { expand(this.id); };
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    New Coder
    Join Date
    May 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by hemebond
    Well, I started going through your code. Cleaning it up, changing it to resemble some form of sense, but it's just not worth it when the way to do it properly is so easy.

    hey there ****, i realize my code is fubar, and i stated that at the end of my post. thanks tons for the help your provided!
    Last edited by joh6nn; 09-15-2004 at 08:49 PM.

  • #7
    Senior Coder
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    2,469
    Thanks
    0
    Thanked 0 Times in 0 Posts
    While I think the tone of some of these responses is condescending (hemebond, that's directed at you -- you could have said it a little more kindly, or not said it at all), that doesn't warrant your language here mrtanooki. If you have a problem with someone's response, handle it with the appropriate language -- this forum is used by all ages, and if that language gets found on here, codingforums will be blocked for all types of users -- meaning, your careless remark can potentially ruin the chances of other younger members getting their questions answered.

    In short, if you can't respond in a mature manner, than one of us will be happy to do that for you, otherwise, let it go, or learn a new method of communciation.
    // Art is what you can get away with. <-- Andy Warhol
    ...:.:::: bradyjfrey.com : htmldog : ::::.:...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •