jquery
  1. jquery-append

JQuery append()

append() is a jQuery method that allows you to add content to the end of an HTML element.

Syntax

The syntax for append() is as follows:

$(selector).append(content)
  • selector: the HTML element to which the content will be appended
  • content: the content to be appended to the HTML element

Use

The append() method is useful when you want to dynamically add content to your HTML page using jQuery. It allows you to easily add new elements, text, or HTML to an existing element.

Example

Here is an example of using append() to add new content to an HTML element:

<!DOCTYPE html>
<html>
<head>
    <title>Using jQuery append() method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>

    <div id="myDiv">
        <p>This is the original content of the div.</p>
    </div>
    
    <script>
        $("#myDiv").append("<p>This is new content added with jQuery append() method.</p>");
    </script>

</body>
</html>
Try Playground

In this example, we have a div element with an id of myDiv, which contains some original content. We use the append() method to add a new p element containing the text "This is new content added with jQuery append() method." to the end of the myDiv element.

Summary

Overall, append() is a useful jQuery method for appending new content to an existing HTML element. It is a powerful tool for dynamically updating your web page without the need for reloading the entire page. Try it out in your next project and see how it can improve the experience for your users!

Published on: