54

How can I add text from a DIV to a textarea?

I have this now:

    $('.oquote').click(function() { 
      $('#replyBox').slideDown('slow', function() {
      var quote = $('.container').text();   
         $('#replyBox').val($('#replyBox').val()+quote);   
        // Animation complete.
      });    
    });
Oliver 'Oli' Jensen
  • 2,631
  • 9
  • 26
  • 40

2 Answers2

84

Just append() the text nodes:

$('#replyBox').append(quote); 

http://jsfiddle.net/nQErc/

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
52

That should work. Better if you pass a function to val:

$('#replyBox').val(function(i, text) {
    return text + quote;
});

This way you avoid searching the element and calling val twice.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 5
    @AlienWebguy: Better than `$('#replyBox').val($('#replyBox').val()+quote);` because it does not search the element and call `val` twice. – Felix Kling Aug 14 '11 at 18:55