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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update array in one function by another function.

Status
Not open for further replies.

sskarth

Programmer
Jul 21, 2010
5
US
Hi,

I have small problem and I am stuck with it for days without any solution.

So I have a function that has an array,

function arrjoin( )
{
var a = new Array (1,1,1,1,1,1,0,0,0,0);
var c = a.join("~");
return (c);
}

I have another function that call the arrjoin(). But before doing so, I get a value from an input field based on which i need to update the array in arrjoin() and save that as the new array(I mean same array but with the update value).

function xxxx( )
{
document.getElementById("graphhide").value = index;
var b = joinarr( );
}

This is my current function. In this before I call joinarr( ) I need to update the array a in joinarr( ) with the value=index, that I am fetching here and save the array. (Haven't implemented yet) This process keeps on occurring every time a index value is generated. Thus in the next iteration the array a in arrjoin( ) show not be array show here but the array with the updates values.

I am eventually transferring the array to server side to do some processing based on current value of the array. Any idea on how to achieve the above situation will be of great help to me. Thank you all in advance for your reply.

-SK
 
Mae your array, global, and use the push() function to add the element to the array.

Code:
var a = new Array (1,1,1,1,1,1,0,0,0,0);

function arrjoin( )
{
var c = a.join("~");
return (c);
}

function xxxx( )
{
 a.push(document.getElementById("graphhide").value);
 var b = arrjoin( );
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you for your reply. The thing is push adds elements to the end. What I actually want to do is, if the the value fetched in fn xxxx( ) is, say value=3, then I need to update Array a[3] to 1 if its 0, or a[3] to 0 if its 1.

And the next time if value=7, then I need to update a[7] = 1 if its currently = 0 or vice versa.

I should have been clear at the beginning. Sorry about the confusion.

-SK
 
Above post continued...

So in the next iteration, the array a[] will have new values rather that the one that was defined at the start. Hope its clear now.

Thanks,
-SK
 
That's easy enough:

Code:
if(a[document.getElementById("graphhide").value]=='0'){a[document.getElementById("graphhide").value]='1';}else{a[document.getElementById("graphhide").value]='0';}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you so much for all your help :) Your valuable inputs helped me solved it at last.

Faced issue with declaring the array as global. The function wasn't recognizing the global array. I was thinking there was something wrong but eventually cleared the cache and it worked. I have no idea why clearing cache helped me solve the issue.

-SK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top