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!

hidden array modified by javascript

Status
Not open for further replies.

werjocks

Programmer
Oct 27, 2003
24
US
Im trying to figure out how to use a hidden variable as an array, modifiable from the client side via javascript calls. What I would like to do is something similar to this this...

<input type="hidden" name="test[]" value="None">
<script type="text/javascript">
document.getElementById('test[0]').value="data0";
document.getElementById('test[1]').value="data1";
alert(document.getElementById('test[0]').value);
</script>

but it doesn't seem to like it. Any suggestions would be appriciated
 
><input type="hidden" name="test[]" value="None">
The name has nothing dynamic. Call it test[] does not make it dynamic. (Besides it is not a very good name.) The whole problem is ill-posed.
 
Hidden variables are strings. There's no real way around that. What you need to do is use split and join to convert the hidden variable to an array, and the array back to a string.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Why not try something like this:

Code:
<input type="hidden" name="test0" value="None">
<input type="hidden" name="test1" value="None">
<input type="hidden" name="test2" value="None">
<script type="text/javascript">

var numelements = 3;

for (var di=0;di<numelements;di++)
  {
  document.getElementById('test' + di).value="data" + di;
  alert(document.getElementById('test' + di).value);
  }
</script>

Lee
 
I think tsdragon's suggestion to use just one hidden field, store the data as a string, and using split (on a unique delimiter) is a more manageable (and scaleable) solution in this case.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
That's exactly what I meant. It's easy to implement and works just fine.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
werjocks, what is the purpose of storing the data this way in a hidden field?
If it is to allow passing of the data to another page fine but if it is just for storing information for use on the page it is an unusually awkward way to go.

I suspect you are intending to use this to pass information to another page.
My suggestion is to use an array and before submitting the page split the array into a delimited string as the others above have suggested. Working with the array will be a lot easier on the page then you can split and store the array into the hidden field just before submitting.

You could use a high ascii character as your delimiter so that you do not run a risk of that delimiter actually being a normal part of any of the fields.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top