The moose likes HTML, CSS and JavaScript and the fly likes Passing an Array to a Javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic
Author

Passing an Array to a Javascript function

Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
I am very new to the JavaScript coding.
In my JSP I am getting data from linked list and looping through the list
to pass the value to a JavaScript function.
Then I have a JavaScript function that retrieves the values and assigns it
to the hidden fields.
Now the problem is
regardless of iteration my function always displays the last value in the array
(Example if I have 2 rows in my array
as for i =0
id =4, grp = HOCKEY sname= John
and for i=1
id = 7 grp = SOCKER sname = Phil)

when I get back the value in my function it displays

ID =7
GRP = S
NAME = P

instead of this

ID =4
GRP = HOCKEY
NAME = John

ID =7
GRP = SOCKER
NAME = Phil

I am not sure why it is doing that.
Gurus please help me.
Or is there any othere way to achieve this.
Thanks in advance.

Here is the code snippet. Please ignore jsp code



if(iterator.hasNext())
{
String name = (String)iterator.next();
LinkedList result = (LinkedList)hMap.get(name);
%>


<tr>

<td width="30%">
<img border="0" src="../images/remote_a.gif" width="11" height="11"> 

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

ParentBean parentBean = (ParentBean)result.get(i);
ChildBean childBean = (ChildBean)parentBean.getCommObj();



%>
<a href="JavaScript:selectProgram(id[<%= i%>] = '<%= childBean.getStudentId()%>',
grp[<%= i%>] = '<%= childBean.getGroupId()%>',
sname[<%= i%>] = '<%= childBean.getName()%>'
);"


>

<% } %>

<b><%=name %></b>


<script>

var id = new Array();
var grp = new Array();
var sname = new Array();


function selectProgram(id,grp,sname) {

for (var i=0; i<id.length; i++) {

alert("Length :"+ id.length);
alert("ID :"+ id[i]);
alert("GRP :"+ grp[i]);
alert("NAME:"+ sname[i]);


document.test.id.value = id[i];
document.test.groupId.value = grp[i];
document.test.name.value = sname[i];
}

self.document.test.submit();
}

<form method="POST" name="test" ACTION="/servlet/Test" method="post" >
<input name=name type="hidden" value="">
<input name=id type="hidden" value="">
<input name=groupId type="hidden" value="">
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
I changed my javascript function call to this
<a href="JavaScript:selectProgram(id[<%= i%>] = new Array('<%= childBean.getStudentId()%>'),
grp[<%= i%>] = new Array('<%= childBean.getGroupId()%>'),
sname[<%= i%>]= new Array('<%= childBean.getName()%> '
);"

Now it dispalys complete value like this
ID =7
GRP = SOCKER
NAME = Phil
But still it displays the last element in an array.
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50677

Please close this duplicate.
Any discussion should occur here.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
 
subject: Passing an Array to a Javascript function
 
Threads others viewed
Unable to retrieve value in JSP
Issue with displaying data back on the page
URGENT Help needed on JavaScript.
How to pass an arrayList from a JSP to a servlet.
Tricky form problem glad for any ideas or tell me its not possible
MyEclipse, The Clear Choice