Solved

javascript - converting from var array to string

Posted on 2004-09-01
5
293 Views
Last Modified: 2010-04-01
I'm using a javascript to populate an array defined as:

var typearray = new Array();

then, I need to set a hidden field in the html to this value.  I tried:
 document.sortForm.typearray.value = typearray;

and:

document.sortForm.typearray.value = typearray.toString();

but when I display the value it shows as [object] and when I try to use it in my servlet the value is garbage.

How do I pass this array as a string by using hidden fields in my html?


0
Comment
Question by:donz4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 11957099
You want to put an array (a collection of objects) into a hidden field (a single object)?

I think you'll need to do something like:

value = '' ;
for( int i = 0 ; i < typearray.length ; i++ )
{
    if( i > 0 )
        value += ',' ;
    value += typearray[ i ] ;
}
document.sortForm.typearray.value = value ;

0
 
LVL 35

Expert Comment

by:TimYates
ID: 11957126
basically you can't do what you're trying to do, so that code will comma separate the values in the array, and set the hidden field to that value

So if you had:

valuearray = new Array( 3 ) ;
valuearray[ 0 ] = 'hello' ;
valuearray[ 2 ] = 'there' ;
valuearray[ 3 ] = 'tim' ;

the hidden field will be set to

"hello,there,tim"
0
 
LVL 27

Expert Comment

by:rrz
ID: 11960167
You could try this javascript array method. I saw this in a book.  
document.sortForm.hidden.value = typearray.join();  
or use
document.sortForm.hidden.value = typearray.join(separator);  
Now, do want us to give code for parsing this value on the JSP side ?       rrz
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11961086
> document.sortForm.hidden.value = typearray.join();  

Nice! :-D
0
 
LVL 11

Expert Comment

by:raj3060
ID: 11966643
Something like this;
var typearray = new Array();
var oSpan = document.getElementById("");
oSpan.innerHTML = typearray [0];  // append each array element to the span's contents
and so on---
Did you get the idea?
--Raj
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
Deploying our service is a grudge match between customer benefits and customer pain. In one corner, rolling out fixes (yay!) and delivering new features (double yay!). In the other corner, training on new features (boo – sounds like work), and chan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question