0

I want to use scriplets inside a javascript function. And inside the scriplet i want to make use of a javascript variable. How can i access javascript variable inside the scriplets?

code:

<script>
window.onload = function(e)
                        {
                           arr  = $('#hidden_field').val();
                           arr  = JSON.parse(arr);
                           for(var i=0;i < arr.length;i++){
                                <% SomeClass class = doSomething(arr[i]);
                                %>
                           }
                        }
</script>

I want to pass the arr[i] value to the doSomething function and get some result. How can i do this?

1
  • You cannot mix Server side scripts with Client side javascript values. In your code, when server side is rendered, the <% portion will be replaced with the output. What you can do is to call through AJAX for each request or move entire foreach loop to server side. Commented Oct 3, 2017 at 18:54

1 Answer 1

1

Why you want to access modal to view directly as per good exercide it's not right to do this.Because you should send your data through response. but if you want to do this then follow below:

you can do one thing assign your JS variable value to any hidden field then access it.

 <input type="hidden" name="test" value="10">

onSubmit form below script will execute -

<% 
  length=request.getParameter("test);
  for(int i=0;i<Integer.parseInt(length);i++){
     SomeClass class = doSomething(arr[i]);
   }
%>
Sign up to request clarification or add additional context in comments.

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.