2

I have an INPUT text box.

As someone types into the INPUT text box, i need it to append/add-to a TEXTAREA value.


Lets say user types '12345' into the text box.

The textarea (default value="Comment: ") will automatically add: 'Comment: 12345'. Adding '12345' as they type.

Ricky
  • 276
  • 1
  • 7
  • 22

3 Answers3

1

Russ Cam is missing an edge case -- you can drag and drop text into and within input fields by highlighting and dragging, which his code does not account for.

Below code that covers this case:

$(function() {

  var areaText = $('#area').val();  

  $('#text').bind('keyup keypress drop', dropfunction() { 
      $('#area')[0].value = areaText + $(this)[0].value;
  });

});

That said, there is still another edge case, which is deleting and manipulating via context menu. As of right now I can't see a way to detect the selection of context menu interaction... You could disable it if that is important using the following code

$('#text').bind('contextmenu', function() { 
    return false
});
slifty
  • 13,062
  • 13
  • 71
  • 109
1

Assuming area is the id of your textarea and text the id of your textbox,

$(function() {

  var areaText = $('#area').val();  

  $('#text').bind('keyup keypress', function() { 
      $('#area')[0].value = areaText + $(this)[0].value;
  });

});
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
0
$('#your_input').bind('keypress', function(event) { 
    var char_code = event.which ? event.which : window.event.keyCode;
    var char = String.fromCharCode(char_code);
    $('#your_textarea').value += char;
});
chaos
  • 122,029
  • 33
  • 303
  • 309
  • That gives me: Uncaught SyntaxError: Unexpected end of input – Ricky May 26 '09 at 22:48
  • Erm, sorry. Left off some termination code. Should be better now. For backspaces you'd have to get a bit fancier yeah. – chaos May 26 '09 at 23:00