/    Sign up×
Community /Pin to ProfileBookmark

convert java array to javascript array

anyone know how to convert a java array to a javascript array? I have the java array created and can read it via javascript but it comes out in the wrong format, one long string, I need it as a select box. Thanks!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Feb 21.2005 — You try this
<i>
</i>var yourArr = new Array(&lt;% YOUR JSP STATEMENT %&gt;);


or

<i>
</i>var yourArr = yourArray.split(",");


Eric
Copy linkTweet thisAlerts:
@BigMagooauthorFeb 21.2005 — Hi Eric,

I tried your suggestion and am now getting an error. Below is what I have written, did I misunderstand you?

//arrays on jsp

<%

Vector stateList = new Vector();

admin.getState(stateList);

Vector terrList = new Vector();

admin.getCanTerr(terrList);

%>

<script language="javascript">

function setOptions(o)

{

//your snippet with reference to vector

var state = new Array(<%= stateList %> );

var select2 = document.geo.select2;

select2.options.length = 0;

if (o == "United States")

{

//reference to new javascript var

select2.options[select2.options.length] = new Option(state);

}

if (o == "Canada")

{

select2.options[select2.options.length] = new Option('<%=terrList%>');

}

if (o == "3")

{

select2.options[select2.options.length] = new Option('Chicken');

select2.options[select2.options.length] = new Option('Fish');

}

}

</script>
Copy linkTweet thisAlerts:
@BigMagooauthorFeb 21.2005 — I have done some more playing around but still not getting it to output correctly can anyone take a look?

<script language="javascript">

function setOptions(o)

{

var state = new Array(<% Vector stateList = new Vector(); admin.getState(stateList);

String stateString = " ";

for (int i = 0; i < stateList.size(); i++) {

String sString = stateList.get(i).toString();

stateString = stateString + sString.trim() ;

}

%> );

var select2 = document.geo.select2;

select2.options.length = 0;

if (o == "United States")

{

select2.options[select2.options.length] = new Option('state');

}

if (o == "Canada")

{

select2.options[select2.options.length] = new Option('<%=terrList%>');

}

if (o == "3")

{

select2.options[select2.options.length] = new Option('Chicken');

select2.options[select2.options.length] = new Option('Fish');

}

}

</script>
Copy linkTweet thisAlerts:
@A1ien51Feb 22.2005 — most likey your state string does not have quotes around the states.

Do a view source on the page to see the code generated. Looking at your server side code does not help unless you can see what it is outputting exactly.

Eric
Copy linkTweet thisAlerts:
@BigMagooauthorFeb 22.2005 — Yes, the quotes are around the value"AL" as opposed to the text "Alabama".
Copy linkTweet thisAlerts:
@BigMagooauthorFeb 22.2005 — Here is the view source for my page, as you can see everything is there just not the right format.

<script language="javascript">

function setOptions(o)

{

var select2 = document.geo.select2;

select2.options.length = 0;

if (o == "United States")

{

select2.options[select2.options.length] = new Option(' <option value="AL">Alabama<option value="AK">Alaska<option value="AZ">Arizona<option value="AR">Arkansas<option value="CA">California<option value="CO">Colorado<option value="CT">Connecticut<option value="DE">Delaware<option value="FL">Florida<option value="GA">Georgia<option value="HI">Hawaii<option value="ID">Idaho<option value="IL">Illinois<option value="IN">Indiana<option value="IA">Iowa<option value="KS">Kansas<option value="KY">Kentucky<option value="LA">Louisiana<option value="ME">Maine<option value="MD">Maryland<option value="MA">Massachusetts<option value="MI">Michigan<option value="MN">Minnesota<option value="MS">Mississippi<option value="MO">Missouri<option value="MT">Montana<option value="NA">National<option value="NE">Nebraska<option value="NV">Nevada<option value="NH">New Hampshire<option value="NJ">New Jersey<option value="NM">New Mexico<option value="NY">New York<option value="NC">North Carolina<option value="ND">North Dakota<option value="OH">Ohio<option value="OK">Oklahoma<option value="OR">Oregon<option value="PA">Pennsylvania<option value="RI">Rhode Island<option value="SC">South Carolina<option value="SD">South Dakota<option value="TN">Tennessee<option value="TX">Texas<option value="UT">Utah<option value="VT">Vermont<option value="VA">Virginia<option value="WA">Washington<option value="WV">West Virginia<option value="WI">Wisconsin<option value="WY">Wyoming');

}

if (o == "Canada")

{

select2.options[select2.options.length] = new Option(' <option value="AL">Alberta<option value="BC">British Columbia<option value="MN">Manitoba<option value="NW">NW Territory<option value="NA">National<option value="NB">New Brunswick<option value="NF">Newfoundland<option value="NS">Nova Scotia<option value="NU">Nunavut<option value="ON">Ontario<option value="PE">Prince Edward Island<option value="QE">Quebec<option value="SP">Saint Pierre & Miquelon<option value="SA">Saskatchewan<option value="YU">Yukon');

}

if (o == "3")

{

select2.options[select2.options.length] = new Option('Chicken');

select2.options[select2.options.length] = new Option('Fish');

}

}

</script>
×

Success!

Help @BigMagoo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...