How to get and set HTML using jQuery html method

jQuery html() method allows you to get content from element or set new content to matched element. This also includes HTML tags. If no any element is matched, it returns undefined.

Syntax:

$(selector).html();

Here is the example below how you can get content and set new content to the element:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div class="container">
        <div id="paragraph">
            <p>Hello Earth!</p>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script type="text/javascript">
        console.log($('#paragraph').html()); // <p>Hello Earth!</p>
        $('#paragraph').html('<p>Hello World!</p>');
        console.log($('#paragraph').html()); // <p>Hello World!</p>
    </script>
</body>
</html>

I hope it will help you.

Tags:

Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

Or

No Comment