0

In my JSP,I have a Java array menuNames ,I want to assign a value from that array to a text area.Here below i mentioned the Javascript code,Please advice me to correct the code..

function setMainMenuURL(index) {

document.getElementById("SelectedURL").value=<%menuNames[index]%>;
}

4 Answers 4

1

You're confusing your Java code (in the JSP) and your JavaScript code. The Java code runs on the server-side, before anything is returned to the browser, and has nothing to do with the JavaScript code. The JavaScript runs on the client-side, in the user's browser, after the Java code has already run.

What you're trying to do - which won't work - is use a JavaScript parameter inside your Java code. Since the Java has already executed, that makes absolutely no sense.

Your best bet would be to print out the Java array as a valid JavaScript array (saving it to a JavaScript variable), then use that in your JavaScript code. Something like this:

var javascriptArrayMenuNames = <%= Java code to output JSON representation of the array %>;
function setMainMenuUrl(index) {
    document.getElementById('SelectedURL').value = javascriptArrayMenuNames[index];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. Can you please explain "Java code to output JSON representation of the array".
1

you can't assign value from java array to javascript function because javascript function is called on client side whereas java is excuted on server side. so create javascript array on serverside with java

Comments

1

Use JSP Expression -

document.getElementById("SelectedURL").value=<%=menuNames[index]%>;

A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file.

Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file.

The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression.

Comments

0

Please take it as a comment to Quoi's answer: this:

document.getElementById("SelectedURL").value=<%=menuNames[index]%>;

will not work. because "index" variable is undefined in the compiled servlet.

2 Comments

Even if you don't yet have the required reputation to comment on other people's questions and answers, posting an answer of your own is not the correct route to take.
@AnthonyGrist I waited for 15 mins, but no one seemed to correct the error in Quoi's answer. So I had to write it off as an answer. Did I have any other way of posting corrections?

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.