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]%>;
}
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: