views:

49717

answers:

4

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 info. Also, how can I convert the keycode returned to an ASCII character?

+2  A: 

Normally, it's the value property

testArea.value

Or is there something I'm missing in what you need?

sblundy
I actually hoping a textchanged event or something along the line would be invoked (I could do this pretty easily with the keyup event anyway). I think i will stick to using the keyup event, but do you know of a way to convert the keycode to an ASCII char? Thanks. :)
RodgerB
+28  A: 

Why would you want to convert key strokes to text? Add a button that sends the text inside the textarea to the server when clicked. You can get the text using the value attribute as the poster before has pointed out, or using jQuery's API:

$('input#mybutton').click(function() {
    var text = $('textarea#mytextarea').val();
    //send to server and process response
});
Eran Galperin
Because the textarea would contain more then just the text needed. (it's console, visualize command prompt). Thanks for info on the val function. :)
RodgerB
The you can process the text using javascript. What is the point of returning the key stroke codes?
Eran Galperin
The point is, imo, obtaining the key stroke code would be more efficient then splitting the textarea by a delimiter (picture a possibly large array of text).
RodgerB
Sorry, I'm failing to visualize the scenario in which that would be true... maybe if you edited your original post and added an example it would help people trying to answer
Eran Galperin
A: 

I have figured out that I can convert the keyCode of the event to a character by using the following function:

var char = String.fromCharCode(v_code);

From there I would then append the character to a string, and when the enter key is pressed send the string to the server. I'm sorry if my question seemed somewhat cryptic, and the title meaning something almost completely off-topic, it's early in the morning and I haven't had breakfast yet ;).

Thanks for all your help guys.

RodgerB
A: 

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

-------------------------------
| consle output ...           |
| more output                 |
| prevous commands and data   |
-------------------------------
> this is a 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

Ped