Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating arrays

Status
Not open for further replies.

plange

Programmer
Dec 12, 2001
20
US
I have the following function, which is called when a link is clicked on the page (there will be a list of names, each of which calls this function). I thought I wrote it so that if the second one is clicked it will add to the array, but it keeps replacing it. What am I doing wrong?

guestlist=null;
function QueueUser(uid,thename) {
document.getElementById('GuestQueue').style.visibility = "visible";
if (!guestlist) {
var guestlist = new Array();
}
var arrLen = guestlist.length;
//alert(arrLen);
var startnum=0;
if (arrLen != 0) {
startnum=arrLen+1;
}
guestlist[startnum]=[uid,thename];
//alert(guestlist[0]);
var vGuestQueue="<table width=100% cellpadding=0 cellspacing=0>";
for(i = 0; i < guestlist.length;i++){

//alert("hello");
vGuestQueue= vGuestQueue + "<tr><td>" + guestlist[1] + "</td><td><a href='javascript:void(0);' onClick='rmvGuest(" + guestlist[1] + ");'>remove</a></tr>";

}
vGuestQueue=vGuestQueue + "</table>";
document.getElementById("GuestQueueList").innerHTML =vGuestQueue;
}
 
Is the page being reloaded between clicks?

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Enable Apps
 
The array guestlist is a local array, everytime you call the function you are manipulating the array guestlist that is defined in the function, not the global variable guestlist, which will always be null

just define guestlist as var guestlist = new Array() outside of your functions in your script tag.

Code:
if (!guestlist) {  <-- Checking global variable (always null)
  var guestlist = new Array();  <-- Create local variable (value will be lost after function ends)
}


<.
 
Thanks Monksnake-- that was a big part of the problem! (the other was the screwy way I was doing the startnum, once I made that eq the array length it worked!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top