jQuery text() method allows you to get text from element or set new text to element.
Syntax:
$(selector).text();
Or set new text to element by passing text into parameter.
$(selector).text('Hello World!');
Here is the example below:
<!doctype html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="m-3">jQuery text() function</h1>
<p id="paragraph">Hello Earth!</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
console.log($('#paragraph').text()); // Hello Earth!
$('#paragraph').text('Hello World!');
console.log($('#paragraph').text()); // Hello World!
});
</script>
</body>
</html>
I hope it will help you.