2

jQuery text method

 1 year ago
source link: https://www.laravelcode.com/post/jquery-text-method
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

jQuery text method

  212 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]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK