jQuery Textarea Value Examples
Have you wondered how to get textarea value using jQuery? In jQuery, textarea value can be easily obtained by using just single line of code. Read on to find out more.
How to use jQuery to Get Textarea Value
Let’s assume that you have a textarea with ID “comments”. You can how get it’s value using jQuery in the following ways:
HTML source:
<textarea name="comments" id="comments"></textarea>
Example 1: Get Textarea value using val()
<script type="text/javascript">
$(document).ready(function() {
var value = $("#comments").val();
alert(value);
});
</script>When you execute the above code, you will see a JavaScript alert but that will be blank because when the page loads, the textarea does not have any content in it. To assign some value to the textarea and get it’s value when the page loads, try the HTML source below and execute the code:
HTML Source:
<textarea name="comments" id="comments">Some content</textarea>
You will now see that the JavaScript alert says: Some content
So that’s how you can get textarea value on document load.
Example 2: Get Textarea value when it’s value is changed using change()
<script type="text/javascript">
$(document).ready(function() {
$("#comments").change(function(){
var value = $(this).val();
alert(value);
});
});
</script>What the above jQuery does is that it alerts the value of the textarea when it is changed. So once you enter something in the textarea, click outside of the textarea so that it loses focus. You will now see the javascript alert with the content you entered in it.
Example 3: Get Textarea value using it’s name attribute
<script type="text/javascript">
$(document).ready(function() {
$("textarea[name='comments']").change(function(){
var value = $(this).val();
alert(value);
});
});
</script>If you have noticed, I am targeting the textarea by using it’s name in the above code. That’s how you can get textarea value using name in jQuery.
Example 4: Get Textarea value using it’s Class
HTML Source:
<input name="comments" type="text" id="comments" class="comments_class" />
<script type="text/javascript">
$(document).ready(function() {
$(".comments_class").change(function(){
var value = $(this).val();
alert(value);
});
});
</script>In the above code, the textarea is targeted using the “comments_class” applied to the textarea. So now whenever the content of the “comments” textarea is changed, its value will be shown in alert.
Example 5: Get Textarea value using blur()
<script type="text/javascript">
$(document).ready(function() {
$("#comments").blur(function(){
var value = $(this).val();
alert(value);
});
});
</script>What the above jQuery does is that it alerts the value of the textarea when it is changed. So once you enter something in the textarea, click outside of the textarea so that it loses focus. You will now see the javascript alert with the content you entered in it. This works same as the change() method in this context.
Simple, isn’t it?
Do you know of any other ways to get textarea value using jQuery? Feel free to share below.