Open In App

How to create an HTML element using jQuery ?

In this article, we will see how to create an HTML element using jQuery. To create and append the HTML element, we use the jQuery append() method. 

The jQuery append() method is used to insert some content at the end of the selected elements.



Syntax:

$(selector).append( content, function(index, html) )

Parameters:



Example: In this example, we will use the append() method of JQuery.




<!DOCTYPE html>
<html>
<head>
    <title>
        How to create an HTML element using jQuery?
    </title>
    <script src="
    </script>
    <!-- Script to add div element in the HTML document -->
    <script>
        $(document).ready(function() {
 
            $("button").click(function() {
                $(".append").append(
'<div class="added">New HTML element added</div>');
            });
        });
    </script>
    <!-- Style to use on div element -->
    <style>
        .added {
            padding: 20px;
            margin-top: 20px;
            background: green;
            color: white;
            display: inline-block;
        }
    </style>
</head>
<body>
    <center>
        <h1 style="color: green;">GeeksforGeeks</h1>
 
        <h3>
            How to create an HTML element using jQuery?
        </h3>
 
        <button id="append">Append New HTML Element</button>
 
        <div class="append"></div>
    </center>
</body>
</html>

Output:


Article Tags :