Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get textarea text

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?

like image 200
RodgerB Avatar asked Sep 28 '08 00:09

RodgerB


People also ask

How to get text of textarea in jQuery?

We can get the value of textarea in jQuery with the help of val() method . The val() method is used to get the values from the elements such as textarea, input and select. This method simply returns or sets the value attribute of the selected elements and is mostly used with the form elements.

How do I get text from textarea?

Use the value property to get the value of a textarea, e.g. const value = textarea. value . The value property can be used to read and set the value of a textarea element. If the textarea is empty, an empty string is returned.

How can get value entered textbox using jQuery?

jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element. In case of the set value, it sets the value of the attribute for all elements.

How do I display textarea content in HTML?

Use the <textarea> tag to show a text area. The HTML <textarea> tag is used within a form to declare a textarea element - a control that allows the user to input text over multiple rows. Specifies that on page load the text area should automatically get focus.


2 Answers

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 }); 
like image 105
Eran Galperin Avatar answered Oct 05 '22 23:10

Eran Galperin


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

get:

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

set:

$('#myTextBox').val('new value'); 
like image 21
Thomas Koelle Avatar answered Oct 05 '22 23:10

Thomas Koelle