Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting JSP with newline character to Javascript

 
venkat warlu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
venkat warlu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic