-3

I know how to use $.each in my program which is written in Java like this

$.each( ['a','b','c'], function(i, l){ 
   alert( "Index #" + i + ": " + l ); 
 });

what about If I want to pass arraylist or string array from my java program to the JQuery any one knows how??

I wrote

String[] arr1=new String[2];
arr1[1]="whatever";
arr1[2]="ksj";
$.each( arr1, function(i, l){ 
   alert( "Index #" + i + ": " + l );
 }

but it does not work?


Update:: I have jsp page which contains part of java as follows:

<%@page import="java.lang.reflect.Array"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

        <style>
            .highlight { background-color: yellow }
        </style>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%  ArrayList wordslist = new ArrayList();
      wordslist.add("Hello");
      wordslist.add("again");

     String[] arr1=new String[wordslist.size()];
      for (int i=0; i<wordslist.size();i++)
      {
     arr1[i]=wordslist.get(i).toString();
        }%>



        <a href="http://jquery.com/">jQuery</a>

        <p> Hello again and again</p>
          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
       <script src="jquery.highlight-3.js"></script>
       <script>

        $(document).ready(function(){
       alert( arr1 );     
      var arr=new Array();
      arr.

$.each( arr, function(i, l){ 
   alert( "Index #" + i + ": " + l );
 }
)


     });

       </script> 
    </body>
</html>

The problem is wordslist arry is dynamic and I want to pass it to the JQuery ?? How can I do that without using inline embedded java please help Is there a way to pass wordslist to arr array in the Jquery!!! This really make me disappointed!!

2
  • 1
    Java or Javascript? That is not valid Java syntax. Commented Jun 29, 2011 at 23:04
  • Java and Javascript are entirely unrelated languages. JQuery is a Javascript library. Commented Jun 29, 2011 at 23:07

3 Answers 3

3

Use a JSON library for Java. Then serialize your ArrayList to JSON:

ArrayList wordslist = new ArrayList();
wordslist.add("Hello");
wordslist.add("again");

String json = (new JSONArray(wordslist)).toString();

Echo the JSON text at the appropriate place in your JavaScript code. E.g.

$(document).ready(function(){    
    var arr = <%= json %>;
    $.each( arr, function(i, l){ 
        alert( "Index #" + i + ": " + l );
    });
});

I'm actually not sure whether this is the right JSP syntax for printing a variable. Please correct if not.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks alot ! but Is there anyway to avoid using embedded java code <% = json %> .
@user651521: No. Jave works on the server side, JavaScript on the client side. You cannot access Java's data structures directly with JavaScript.
0

Well your code is formatted wrong, for starters. You're missing the closing bit. Second, indexes are zero-based. Third, you created the array wrong.

var arr1 = new Array();
arr1[0]="whatever";
arr1[1]="ksj";
$.each( arr1, function(i, l){ 
   alert( "Index #" + i + ": " + l );
}); //Missed the );

Overall, I recommend writing JavaScript rather than Java when using jQuery. Just a tip. :)

1 Comment

sorry ! My array is in the java part of the program and it is changable some times it contains 2 items sometimes 3 items and so on.. the problem is how to load the array in the jquery with the array in the java program without using assignment?
0

Javascript variables are not declared with a type, and you cannot ask for an array of String, because Javascript only has one kind of array, which holds anything. Also, make sure your brackets balance. Finally, array indices in Javascript start at 0, just like in most other programming languages (including Java, which you seem to be getting confused with - but Java and Javascript have nothing to do with each other, despite the names).

var arr1 = [];
arr1[0]="whatever";
arr1[1]="ksj";
$.each( arr1, function(i, l){ 
   alert( "Index #" + i + ": " + l );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.