Reputation: 241
Is it possible to run javascript functions inside jsp tags? I'd like to run a sudden function as many times as there's objects in my ArrayList. Below doesen't work, but I hope it gives an idea of what I'm trying to achieve.
<script>
function test(){
alert();
}
</scripts>
<%
ArrayList<Marker> list = new ArrayList<Marker>();
list = (ArrayList<Marker>)request.getAttribute("markers");
for(int i = 0; i < list.size(); i++){
%>
<script>
<%
test();
%>
</script>
<%
}
%>
Is it possible to do it with something like ?
<c:forEach var="name" items="${markers}">
<%-- call my javascript function --%>
</c:forEach>
Upvotes: 5
Views: 52719
Reputation: 295
By writing your js code within the script tag inside a out.println() has shown below:
<%
ArrayList<Marker> list = new ArrayList<Marker>();
list = (ArrayList<Marker>)request.getAttribute("markers");
for(int i = 0; i < list.size(); i++){
out.println("<script>test();</script>");
}
%>
Upvotes: 0
Reputation: 524
<%
ArrayList<Marker> list = new ArrayList<Marker>();
list = (ArrayList<Marker>)request.getAttribute("house");
for(int i = 0; i < list.size(); i++){
%>
<script>
test('<%= list.get(i).name %>');
<script>
<%
}
%>
<script>
function test(i){
alert(i);
}
</script>
Upvotes: 2
Reputation: 1258
Below correction in your code will work fine for you
<script>
function test(){
alert("Hello"); // added sample text
}
</script>
<%
ArrayList<Marker> list = new ArrayList<Marker>();
list = (ArrayList<Marker>)request.getAttribute("markers");
for(int i = 0; i < list.size(); i++){
%>
<script>
test(); //No need to put java script code inside scriptlet
</script>
<%
}
%>
Upvotes: 8