Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a .jsp page in which I used on click JavaScript function for further validation and other processing stuff. Now I've created an array in jsp scriptlets and how do I pass into this JavaScript function that gets called when submit button is hit? I tried something like this:

var jsArray = <%=lineArray%>;

But that didn't work out.
In fact the function wasn't getting called after I the put above scriptlet. So how do I copy this java array into a JavaScript array?

share|improve this question
2  
Google something like AJAX and Client-Server principle. –  reg4in 14 hours ago
    
Share your code snippet –  Susai 14 hours ago

1 Answer 1

Try something like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

var jsArray = [ 
  <c:forEach var="item" items="${lineArray}">
   <c:out value="${item}"/>,
  </c:forEach>
];

This will generate javascript array variable jsArray and put the Java lineArray values into it. If that's what you need to do.

share|improve this answer
    
What is prefix="c" about? And what is <c:? Is it created due to the prefix ="c" declaration? –  swipe hard 13 hours ago
    
You can use custom jsp tags from custom taglibs in .jsp. In this example I'm using standard JSTL taglib. See tutorialspoint.com/jsp/taglib_directive.htm or tutorialspoint.com/jsp/jsp_standard_tag_library.htm for more info. –  daerin 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.