Get textarea value in jQuery

textarea value using jQuery

In web applications while using textarea control, we need to get the value of textarea at client side. In this post we will see example of how to get value of textarea in jQuery.

Example to get textarea value in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="development-bundle/jquery-1.7.1.js" type="text/javascript"></script>
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<title>Get textarea value in jQuery</title>
<script type="text/javascript">
$(document).ready(function () {
$("#submitbtn").click(function () {
var textAreaValue = $("#txtMessage").text();
alert(textAreaValue);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<textarea cols="50" rows="5" id="txtMessage"></textarea>
<input type="button" id="submitbtn" value="Submit" />
</div>
</form>
</body>
</html>

4 comments :

  1. thank you my friend, this tutorial help me

    ReplyDelete
  2. This is not working :(

    ReplyDelete
  3. Instead of
    var textAreaValue = $("#txtMessage").text();
    put
    var textAreaValue = $("#txtMessage").val();

    ReplyDelete
  4. If you use Chrome Browser or want users to be able to use crome you probabli need to add:

    header("X-XSS-Protection: 0");

    Or the XSS protection in Chrome will not execute the script:
    The XSS Auditor refused to execute a script in http://... because its source code was found within the request. The auditor was enabled as the server sent neither an 'X-XSS-Protection' nor 'Content-Security-Policy' header.

    ReplyDelete