jQuery text() method allows you to get text from element or set new text to element.
$(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/[email protected]/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.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to get substring from a string using jQuery
Use the JavaScript substring() ...How to Abort Ajax Requests using jQuery
Sometimes you might want to cancel the a...How to Remove an Event Handler in jQuery
Use the jQuery off() Method You can s...How to replace a word inside a string in PHP
Use the PHP str_replace() func...How to call a function automatically after waiting for some time in jQuery
Use the jQuery delay() method You can...