0

I've been working on a project that involves ajax; it's a planner for school assignments. When a button is pressed, it's supposed to change the text inside of 31 <textarea>'s (and one <span>) based on data it gets from the server. The thing is, textareas that have been changed after the last time the window was refreshed don't change. I've looked over the JSON sent between the server and the webpage and vice-versa, and concluded that the bug is in the success function of the ajax call. Here's the code:

 success: function(data) { 
 $("span#date").text(data['date']);
 $("#assignments").find("textarea").each(function() {
 $(this).text("");
 $(this).html(data[$(this).attr("id")]);
 });
 console.log(data); // I was using this to see if the data received from the server was correct
 }

Thanks very much in advance for any help.

asked Sep 1, 2012 at 19:35
4
  • Are the ids in data synchronized with the ones on your textareas? Commented Sep 1, 2012 at 19:42
  • Works for me: jsfiddle.net/tLsHq/1. Commented Sep 1, 2012 at 19:43
  • Yes they are. If I refresh the page, everything works properly. But it's not really practical for someone to refresh the page every time they enter something in the textarea to make sure it works properly; it's the reason I used AJAX in the first place. Commented Sep 1, 2012 at 19:43
  • 1
    Just for the record, even with .val this can be simplified a lot: $("#assignments textarea").val(function() { return data[this.id]; }). Commented Sep 1, 2012 at 19:51

2 Answers 2

1

you should use .val() for textarea as it's basically an input.

You can't really have html elements inside it.

answered Sep 1, 2012 at 19:39
Sign up to request clarification or add additional context in comments.

2 Comments

I had .val() there before, and the same thing was happening. I tried .text() and .val() and .html() just to see if anything different happened, but the same thing happened each time. Thanks though, I'll try .val() again.
.html works on a textarea, although it's not really appropriate.
0

Try $(textarea).val() instead of .html(). I noticed that html only works the first time the textarea is rendered.

answered Sep 1, 2012 at 19:45

1 Comment

Thanks! This ended up working. I wonder why .val() didn't work the last time I tried it... Guess I changed some other code. Thanks again!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.