JQuery+Java setting field value with val() + single quote
- by Fabio K
I have a problem setting the value of a textarea element with jquery's val().
Basically, I have a JSP file which receives a string parameter called 'text'.
Java code:
String text = (String) request.getParameter('text');
Now I want my textarea element to receive this text:
Javascript code:
$('#textarea_id').val('<%=text%>');
It works when my text doesnt contain quotes single quotes (and possibly other chars).
For example, for text =
test'
this error happens:
Uncaught SyntaxError: Unexpected token ILLEGAL
$('#textarea_id').val('test'');
I hope you guys understand. I need a way to encode this value... i tried using escape so the quote is replaced by %27, but after unescaping its replaced again and the error happens.
Thanks!