jQuery
Check if Textarea is Empty using jQuery Examples
Have you ever wondered how to check if Textarea is Empty using jQuery? In this article, I am going to share a very easy way that you can use to check if Textarea is Empty using jQuery.
How to check if Textarea is Empty using jQuery – Examples
For this example, I am assuming that we have a Textarea in a form and we need to check if this textarea is empty or not when the “Check” button is clicked. The following is the complete source code:
Example: Use jQuery to check if textarea is empty
<html>
<head>
<title>jQuery: Check if Textarea is empty</title>
<script type="text/javascript" src="js/jquery_1.7.1_min.js"></script>
</head>
<body>
<form name="form1" method="post" action="">
<label for="comments">Comments:</label>
<textarea name="comments" id="comments"></textarea>
</form>
<button id="check">Check</button>
<script type="text/javascript">
$('#check').click(function() {
if ( $('#comments').val() == '' )
{
alert('Empty!!!');
}
else
{
alert('Contains: ' + $('#comments').val() );
}
});
</script>
</body>
</html>Once you run the above code and click the “Check” button, the page will alert “Empty!!!”. If you enter something in it and then click the button, it will show the value you entered.
Simple, isn’t it?
Do you know of any other ways to check if textarea is empty using jQuery? Feel free to suggest by commenting below.
Ooh very nice… It solved my problem. Thanks dear
May 7, 2018 at 3:02 am