1

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.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
andrewheins
  • 788
  • 2
  • 9
  • 17

4 Answers4

5

Set .val() instead of .text()

Stackoverflow Archive:

Community
  • 1
  • 1
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 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. – Sampson Jun 29 '09 at 17:01
  • Yeah, site:stackoverflow.com 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 Jun 29 '09 at 22:00
  • 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.stackexchange.com/questions/879/make-the-search-box-return-results-from-all-stackoverflowian-sites/925#925 – Sampson Jun 30 '09 at 02:08
3

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
  • 524,688
  • 99
  • 697
  • 795
  • Well dang. I thought that was only for tags that actually have "input" in the tag name. Worked great. Thanks :) – andrewheins Jun 29 '09 at 16:48
  • 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 Jun 29 '09 at 16:56
0

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

$("input[name='db_entries']").click(function () {
    $("#outputarea").load("?", {"paramName":this.value});
});
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • 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 Jun 29 '09 at 18:10
-2

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

function(data){
  $("textarea").attr("value",data);
  alert('done');
}
Sampson
  • 265,109
  • 74
  • 539
  • 565
Thomas
  • 4,889
  • 4
  • 24
  • 19