jQuery text method

jQuery text() method allows you to get text from element or set new text to element.



Or set new text to element by passing text into parameter.

$(selector).text('Hello World!');

Here is the example below:

<!doctype html>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <div class="container">
        <h1 class="m-3">jQuery text() function</h1>
        <p id="paragraph">Hello Earth!</p>
    <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!

I hope it will help you.