JQuery+Java setting field value with val() + single quote

Posted by Fabio K on Stack Overflow See other posts from Stack Overflow or by Fabio K
Published on 2012-10-17T16:58:02Z Indexed on 2012/10/17 17:00 UTC
Read the original article Hit count: 190

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery