View Full Version : problem keeping a value set

09-12-2004, 10:53 PM
its me again, and i'm still working on the same code.

assume menu.length = 3;

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:

<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.onmouseover = function() { expand(menulen); };
menuitemname = document.createTextNode(menu[menulen]);
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");
submenuitemname = document.createTextNode(submenu[menulen][submenulen]);



// this is whurr we store teh submenu items.

tempvar = submenulist;


function expand(menuID) {

<body onload="buildmenu()">
<div id="menu">

<div id="gay">

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?

09-13-2004, 07:33 AM
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.

09-13-2004, 04:07 PM
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. :(

09-13-2004, 11:55 PM
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.

09-14-2004, 03:40 AM
Since you set menulen as the id of the link, use the id as the parameter to the expand function:

linkelement.onmouseover = function() { expand(this.id); };

09-15-2004, 05:43 PM
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!

09-15-2004, 09:01 PM
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.