Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Peter Rooke
Bartenders:

Passing an Array to a Javascript function

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Sheriff
Posts: 67762
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please close this duplicate.
Any discussion should occur here.
    Bookmark Topic Watch Topic
  • New Topic