Add text to textarea - Jquery

2019-01-18 01:20发布

问题:

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.
      });    
    });

回答1:

Just append() the text nodes:

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

http://jsfiddle.net/nQErc/



回答2:

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.