How Can I Use Append Method In JQuery With Example

admin_img Posted By Bajarangi soft , Posted On 13-10-2020

In jQuery append() method inserts content AT THE END of the selected HTML elements.How to use append method

How Can I Use Append Method In JQuery With Example

Step 1:Create index.html file and implement below code.

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<ol>
    <li>List item 1</li>
    <li>List item 2</li>
    <li>List item 3</li>
</ol>

<button  class="btn btn-primary" id="btn1">Append text</button>
<button  class="btn btn-primary" id="btn2">Append list items</button>


Step 2:Implement jquery to use append method.

<script>
    $(document).ready(function(){
        $("#btn1").click(function(){
            $("p").append(" <b>Appended text</b>.");
        });

        $("#btn2").click(function(){
            $("ol").append("<li>Appended item</li>");
        });
    });
</script>


Complete Code For Append Method In JQuery
<!DOCTYPE html>
<html>
<head>
    <title>How Can I Use Append Method In JQuery With Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
    body {
        background: darkolivegreen;
    }

</style>
<body>
<div class="container">
    <br><br><br>
    <div class="text-center">
        <h2 id="color" style="color: White"> Append Method In JQuery</h2>
    </div>
    <br>
    <br>

    <div class="well">
        <p>This is a paragraph.</p>
        <p>This is another paragraph.</p>

        <ol>
            <li>List item 1</li>
            <li>List item 2</li>
            <li>List item 3</li>
        </ol>

        <button  class="btn btn-primary" id="btn1">Append text</button>
        <button  class="btn btn-primary" id="btn2">Append list items</button>
    </div>

</div>
</body>
</html>
<script>
    $(document).ready(function(){
        $("#btn1").click(function(){
            $("p").append(" <b>Appended text</b>.");
        });

        $("#btn2").click(function(){
            $("ol").append("<li>Appended item</li>");
        });
    });
</script>

Related Post