jQuery text method

  211 views   7 months ago jQuery

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/[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.

Author : Harsukh Makwana
Harsukh Makwana

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]