jQuery

How to get the value of a textarea in jQuery

In this tutorial, we are going to see how to get the value of a textarea in jQuery. You can simply use jQuery’s val() method to get the value of a textarea. Make sure to remove all leading and trailing white space, otherwise unexpected results may occur.
 

HTML Code:
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Get the value of a textarea in jQuery</title>
		<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
		<script>
			// Put the jQuery code here
		</script>
	</head>
	<body>
		<textarea id="val" rows="4" cols="70"></textarea>
		<button type="button">Get the value</button>
	</body>
</html>
 

jQuery Code:
$(document).ready(function() {
    $("button").click(function() {
        var val = $.trim($("#val").val());
        if (val != "") {
            alert(val);
        }
    });
});
Result
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *