Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Create a DIV Element Dynamically Using jQuery</title> <style> .container{ padding: 20px; background: #f2f2f2; border: 5px solid #aaa; } .content{ padding: 10px; border: 3px solid orange; } </style> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function(){ // Insert elements on click of the button $("button").click(function(){ // Creating a div element at the end $(".container").append('<div class="content">Appended DIV</div>'); // Creating a div element at the start $(".container").prepend('<div class="content">Prepended DIV</div>'); }); }); </script> </head> <body> <div class="container"> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p><button type="button">Insert DIV</button></p> </div> </body> </html>