jQuery get textarea text

By : RodgerB
Source: Stackoverflow.com

Recently I have started playing with jQuery, and have been following a couple of tutorials. Now I feel slightly competent with using it (it's pretty easy), and I thought it would be cool if I were able to make a 'console' on my webpage (as in, you press the ` key like you do in FPS games, etc.), and then have it Ajax itself back to the server in-order to do stuff.

I originally thought the best way would be to just get the text inside the textarea, and then split it, or should I use the keyup event, convert the keycode returned to an ASCII character, append the character to a string and send the string to the server (then empty the string).

I couldn't find any information on getting text from a textarea, all I got was keyup information. Also, how can I convert the keycode returned to an ASCII character?

By : RodgerB


You should have a div that just contains the console messages, that is, previous commands and their output. And underneath put an input or textarea that just holds the command you are typing.

| consle output ...           |
| more output                 |
| prevous commands and data   |
> This is an input box.

That way you just send the value of the input box to the server for processing, and append the result to the console messages div.

By : Ped

Where it is often the text function you use (e.g. in divs etc) then for text area it is val




$('#myTextBox').val('new value');

Methinks the word "console" is causing the confusion.

If you want to emulate an old-style full/half duplex console, you'd use something like this:

    $.get("url", { keyCode: event.which }, ... );
    return true;

event.which has the key that was pressed. For backspace handling, event.which === 8.

By : eric

This video can help you solving your question :)
By: admin