How to get the value of div content using jQuery

How to get the value of div content using jQuery


In this tutorial, you will be learning how to get the value of div content using jquery.

We are going to Get the value with jQuery text() method and html() method as given below example:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to get value of div content using jQuery</title>
</head>
<body>

    <div id="divContent">
        This is a div Tag
        <h5>This is a Heading Tag</h5>
        <p>This is a paragrah Tag</p>
    </div>

    <button type="button" id="myButton">Click Me</button>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function () {

            $('#myButton').click(function (e) {
                e.preventDefault();

                var div_content_text = $('#divContent').text(); //text() Method
                alert(div_content_text);

                var div_content_html = $('#divContent').html(); //html() Method
                alert(div_content_html);

            });

        });
    </script>
</body>
</html>


Thanks for reading.

© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT