jQuery get textarea text
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
});
how to get the value of a textarea in jquery?
Value of textarea is also taken with val
method:
var message = $('textarea#message').val();
console.log(message);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea cols="60" rows="5" id="message" name="message">
Hello,
world!
</textarea>
val() vs. text() for textarea
The best way to set/get the value of a textarea is the .val()
, .value
method.
.text()
internally uses the .textContent
(or .innerText
for IE) method to get the contents of a <textarea>
. The following test cases illustrate how text()
and .val()
relate to each other:
var t = '<textarea>';
console.log($(t).text('test').val()); // Prints test
console.log($(t).val('too').text('test').val()); // Prints too
console.log($(t).val('too').text()); // Prints nothing
console.log($(t).text('test').val('too').val()); // Prints too
console.log($(t).text('test').val('too').text()); // Prints test
The value
property, used by .val()
always shows the current visible value, whereas text()
's return value can be wrong.
Getting Textarea Value with jQuery
you have id="#message"
... should be id="message"
http://jsfiddle.net/q5EXG/1/
Cannot get textarea value loaded with ajax using jquery in codeigniter
To get the value of textarea you need to use .val()
instead of .text()
like:
var comments = jQuery('textarea#comments').val();
console.log( comments );
As mentioned in the docs:
The
.text()
method cannot be used on form inputs or scripts. To set or get the text value ofinput
ortextarea
elements, use the.val()
method. To get the value of a script element, use the.html()
method.
DEMO:
// Set textarea value$('#comments').val('This is some text here');
// Get textarea valuevar comments = $('#comments').val();console.log( comments );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><h1>A Textarea Element</h1><textarea id="comments" rows="4" cols="50"></textarea>
jQuery - find text in textarea
$('textarea').keyup(function() {
if ($(this).val().toLowerCase().indexOf('viber') != -1) {
$(this).val($(this).val().replace(/viber/i, ''));
alert('never say "Viber" again!');
}
});
And pay attention to the String.indexOf
- it returns index of the first match or -1 if it didn't find anything. In your case, you're checking for zero - it's wrong because zero means that it finds first occurence in the begginning of string.
Related Topics
JavaScript Function Order: Why Does It Matter
Detect iPad Users Using Jquery
How to Calculate 3D Rotation on X and Y Axis from a 4X4 Matrix
Detecting Line-Breaks with Jquery
How to Add Predefined Length to Audio Recorded from Mediarecorder in Chrome
Three.Js:2Xmeshes Using Same Vector as Position
How to Stop a JavaScript for Loop
Internal/Modules/Cjs/Loader.Js:582 Throw Err
How to Sort an Array Without Mutating the Original Array
Convert Js Object to Form Data
What Is "Callback Hell" and How and Why Does Rx Solve It
JavaScript Audio Play on Click
How to Parse a Url Query Parameters, in JavaScript
Destructuring and Rename Property
JavaScript - Are Dom Redraw Methods Synchronous