How can I "read" value from a Bean into a javascript variable?

Hello,

I have a short javascript to dynamicly display the results matches the keywork user typed in.

<head>

<script type="text/javascript">

var list = new Array();

list[0] = "Abc";

list[1] = "abbbc";

list[2] = "rasff";

list[3] = "raff";

list[4] = "aalex";

list[5] = "AaLex";

list[6] = "wdas";

function doFilter()

{

var result = new Array();

var count = 0;

var filter_value="^" + document.getElementById("filter").value;

filter_value = filter_value.replace(/\*/g, ".*");

var expr = new RegExp(filter_value, "i");

for (i=0;i<list.length;i++)

{

if (expr.test(list)) {

result[count] = list;

count++;

}

}

document.getElementById("list").value=(result.sort()).join("\n");

}

></script>

</head>

<body>

Filter:

<input type="text" id="filter" onkeyup="doFilter()">

<textarea cols="40" rows="5" id="list"></textarea>

</body>

As you can see right now I am using a testing list "list" I created, now I want to load the list (of user names) from backend database. I have my bean called "userBean" and it has the property "name".

So far I am able to build and display the whole list using <logic:iterator>, but how do I refer to this list? assign the "id" like id="userlist"?

Also my main question would be: how can I refer to this list in my javascript code? i.e., something like var list = new Array(window.getById("userlist"))? I don't know much about javascript with its predefined variables.

If I can not access information back and forth with javaBean and javascript, can someone show me another alternative how I can make this happen please?

[1864 byte] By [fab3a] at [2007-11-27 8:17:53]
# 1

w0w previous post looks messy, here is a better one.

Hello,

I have a short javascript to dynamicly display the results matches the keywork user typed in.

<head>

<script type="text/javascript">

var list = new Array();

list[0] = "Abc";

list[1] = "abbbc";

list[2] = "rasff";

list[3] = "raff";

list[4] = "aalex";

list[5] = "AaLex";

list[6] = "wdas";

function doFilter() {

var result = new Array();

var count = 0;

var filter_value="^" + document.getElementById("filter").value;

filter_value = filter_value.replace(/\*/g, ".*");

var expr = new RegExp(filter_value, "i");

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

if (expr.test(list)) {

result[count] = list;

count++; }

}

document.getElementById("list").value=(result.sort()).join("\n");

} >

</script>

</head>

<body>

Filter: <input type="text" id="filter" onkeyup="doFilter()">

<textarea cols="40" rows="5" id="list"></textarea>

</body>

As you can see right now I am using a testing list "list" I created, now I want to load the list (of user names) from backend database. I have my bean called "userBean" and it has the property "name". So far I am able to build and display the whole list using <logic:iterate>, but how do I refer to this list? assign a "id" like id="userlist"?

my main question would be: how can I refer to this list in my javascript code? i.e., maybe something like var list = new Array(window.getById("userlist"))?

If I can not access information back and forth with javaBean and javascript, can someone show me another alternative how I can make this happen please? Again what I want is to dynamicly display search results when user enters some keywords (I will not query databse everytime but instead I want to store the list inside the jsp page with scope="page")

fab3a at 2007-7-12 20:03:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

make it as arraylist and put into session and then in your js function do this.

<%

ArrayList myList = (ArrayList)session.getAttribute("userList");

if (myList !=null){

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

{

String name = (String)myList.get(i);

%>

var name1 = <%= name %>;

<%

}

%>

skp71a at 2007-7-12 20:03:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Javascript can not access java directly.

If the list is not too large you may want to convert it to a javascript array - ie produce javascript onto the page which creates the array in javascript memory. However that means you are sending the entire list with the html on every request.

Another possible solution would be to use an Ajax call. This would make a request to the server, passing the value of the field, comparing it to your list on the server end, and returning matches in xml.

Hope this helps,

evnafets

evnafetsa at 2007-7-12 20:03:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...