views:

178

answers:

4

Trying to get current data from textarea with jquery and replace it with what server returns. so far q value is picked up right, instead of updating it with server returned content, it just replaces it with ' '

q = $('textarea#id_message').val();
alert(q)
$("textarea#id_message").val(' ').load( '/process/url?html=' + q );

Update

I just tried it passing server data to test results <div>, this does work, <textarea> does not.

$( '#results' ).html( '&nbsp;' ).load( '/process/url?html=' + q );
A: 

I am guessing the load command is failing because you are passing the value of the textarea to as a GET parameter without any sanitation.

using escape(q) might help, but we'd need to know what happens server side to tell for sure.

Pekka
escape(q) did not work, the server side seems like it is working fine
bocca
Can you show an example value of q?
Pekka
<html><head><style type="text/css" media="screen">p{ margin: 0px 0px 6px 0px; font-size: 12px;}</style></head>q returns value<body><p>Hello, World!</p></body></html>
bocca
This is a bit much for a GET request. Can't you send it via a POST Ajax Request?
Pekka
and what does `/process/url?` return, given this?
Skilldrick
i have update post, it works test div, not textarea
bocca
A: 

I wonder if the val() is causing problems. Does it work if you remove the .val('&nbsp;') altogether?

Skilldrick
removing .val(' ') did not work either
bocca
A: 

Shouldn't you just use text or html function instead of val for textarea? AFAIR textarea doesn't have val attribute. It's just <textarea>Text inside textarea</textarea>

Piotr Jakubowski
A: 

This is because when you use .load() on an element, you're calling .html() on the same element later to put the response in there (jQuery core source code here), but that doesn't work on inputs, you need .val(). To do this, you can use $.get() with a callback, like this:

var val = $('#id_message').val();
$.get('/process/url?html=' + val, function(data) {
  $('#id_message').val(data);
});

You could also sanitize the input if the server accepts POST by passing the params as an object like this:

$.get('/process/url', {html: $('#id_message').val()}, function(data) {
  $('#id_message').val(data);
});
Nick Craver