This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes JSP and the fly likes Setting JSP with newline character to Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Setting JSP with newline character to Javascript" Watch "Setting JSP with newline character to Javascript" New topic
Author

Setting JSP with newline character to Javascript

venkat warlu
Greenhorn

Joined: Mar 05, 2008
Posts: 7
Hi all, Thanks in advance. I am trying to set a text area string with newline characters in it to a java script variable using jsp like below:

document.form.textarea.value = '<%=jspStringVariable%>';

But it is throwing error like "Unterminated String Constant" error. How to set the jsp value to my java script.

Actually i am trying to retain my value in textarea while i self submit the form to reload some other option values.

Regards
Venkat

Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19691
    
  10

You should modify that string by replacing each of the newline characters by the string "\n", which is the Javascript escape for the newline.
venkat warlu
Greenhorn

Joined: Mar 05, 2008
Posts: 7
Paul, I am trying to do something like this:

<% String str ="";
for(int i=0; i < TempStr.length(); i++){
if(Character.getNumericValue(TempStr.charAt(i))<0){
str = str + "\n";
}else{
str = str + TempStr.charAt(i);
}
}
%>

And then in JS:
<script>
function fn(){
document.form.textarea.value = '<%=str%>';
}
</script>

It is still not working.

Regards
Venkat

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 63052
    
  69

Have you looked at the HTML and script that's being sent to the client? Is it correct? if not, what can you do to correct it?

Just saying "it doesn't work" isn't helping anyone help you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
venkat warlu
Greenhorn

Joined: Mar 05, 2008
Posts: 7
Thanks Bear for the code tag link. I worked it out myself. I replaced character by character as suggested by Paul. Thanks for your help.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Setting JSP with newline character to Javascript
 
It's not a secret anymore!