tags:

views:

176

answers:

4

I'm using $().post and php to change the contents of a <textarea>.

The script is succeeding - firebug clearly shows that the text in between the textarea tags has changed, and my little alert fires.

The user, however, doesn't see the changes. In Firefox the change doesn't occur at all, and in IE, the textarea updates up to 10 seconds late.

Here's the jquery I'm using:

$(document).ready(function() {
 $('#pv_list li:first').addClass('hilite');
 $("input[name='db_entries']:first").attr('checked', 'checked');
 $("input[name='db_entries']").click(function () {
  $.post("changeEntry.php", {post: $(this).val()}, function(data) { 
   $("textarea").text(data);alert('done');
  });
  $('#pv_list li').removeClass('hilite');
  $(this).parent().addClass('hilite');
 });
});

At first I thought it was because the page didn't validate, but it validates xhtml transitional.

The thing that's really bugging me is I had it working earlier and can't figure out what I changed.

+3  A: 

Have you tried using val() to set the value of the textarea instead of text()?

$.post("changeEntry.php",{post: $(this).val()},
      function(data) {
          $("textarea").val(data);
          alert('done');
});
tvanfosson
Well dang. I thought that was only for tags that actually have "input" in the tag name. Worked great. Thanks :)
andrewheins
Just think about how you'd do it with the normal DOM - it's still textarea.value - not textarea.nodeValue or textarea.data or anything else.
Peter Bailey
+4  A: 

Set .val() instead of .text()

Stackoverflow Archive:

Jonathan Sampson
+1 for the "Stackoverflow Archive" format. :P
Paolo Bergantino
+1 for the links
Peter Bailey
Thanks guys. It's incredibly simple to do a site-search on google, so I'm trying to add an "archive" portion to any questions similar to past-subjects that google turns up.
Jonathan Sampson
Yeah, site:stackoverflow.com <search terms> is about a million times better than the built-in site search. I'm still waiting for them to wise up to this and integrate google search. Isn't there an API for it?
Paolo Bergantino
Paolo, yes, they could have it implemented rather easily - I actually set it up for a client today on their site. That being said, you can search all of the SO sites simultaneously from Firefox : http://meta.stackoverflow.com/questions/879/make-the-search-box-return-results-from-all-stackoverflowian-sites/925#925
Jonathan Sampson
A: 

I'm not sure, but one possibility is that you should do:

function(data){
  $("textarea").attr("value",data);
  alert('done');
}
Thomas
A: 

Wouldn't it be easier to use the load() function?

$("input[name='db_entries']").click(function () {
    $("#outputarea").load("?", {"paramName":this.value});
});
VolkerK
Why? `
Jonathan Sampson
The whole purpose of the onclick handler is to set the contents of the textarea element, isn't it? And that's what load() does.
VolkerK