2

I would like to load some text retrieved from a dB into a textarea. The user clicks a link:

<a class="editlink" id="<?php echo $review['id']; ?>" href="#"><?php echo $review['title']; ?></a>

JQuery passes the ID to GO.PHP:

$(".editlink").click(function() {
    $.get("go.php", {param: $(this).attr('id')}, 
        function(data) {
            $('textarea#area1').html(data);
        });    
    return false;
});

GO.PHP retrieves the text from the dB:

$qry = mysql_query("SELECT * FROM reviews WHERE id = ".$_GET['param']." "); 
while($review = mysql_fetch_array($qry)) {
    echo $review['description'];
} 

As confirmed by Firebug consolle, ID and the text are retrieved correctly. The probelm is that I'm not able to place the text into the textarea:

<textarea id="area1" rows="30" cols="55"></textarea>

I tried: .html(data), .text(data), .val(data) but none display anything. (Please note that the text in the dB may contain HTML tags that I would like to keep).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Nicero
  • 4,181
  • 6
  • 30
  • 52

3 Answers3

6

You need to set the text areas value.

$("#textareaID").val("value of text area");

I put this into a blank HTML doc referenced query in the head and it worked fine.

<form>
  <textarea id="test"></textarea>
</form>
<script>
    $('#test').val('testing');
</script>
thenetimp
  • 9,487
  • 5
  • 29
  • 42
  • I did tried "$('textarea#area1').val(data);" but it does not display anything. – Nicero Feb 05 '12 at 16:54
  • I did try your code, and it all worked for me. Since my answer was accepted did you fine the problem or did someone else accept it? – thenetimp Feb 05 '12 at 17:08
1

have you tried this

$('textarea#area1').attr('value',data);
Lokesh Yadav
  • 1,574
  • 5
  • 23
  • 50
  • Hi, I don't really understand. What's 'value'? 'data' is go to be filled by "echo $review['description'];" and, as said, Firebug consolle returns that the output is corrent. So what's 'value'? – Nicero Feb 05 '12 at 16:53
  • check this post. may be of some help. http://stackoverflow.com/questions/415602/set-value-of-textarea-in-jquery – Lokesh Yadav Feb 05 '12 at 17:00
0

I tried: .html(data), .text(data), .val(data) but none display anything.

As other answers have stated, .val(data) or .text(data) should work. Don't .html(data) with textarea, because won't work in internet explorer however, as it will remove html when used with textareas (try this in IE). I would use the firebug console to ensure data is set, and look at your net tab to ensure everything is returning correctly. My guess is there is an error elsewhere.

Andy E
  • 508
  • 8
  • 15