Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Creating Bootstrap 4 Collapsible Element via JavaScript</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script> <script> $(document).ready(function(){ $(".btn").click(function(){ $("#myCollapsible").collapse('toggle'); }); }); </script> <style> p{ background: #eee; border: 1px solid #ccc; margin: 20px 0; padding: 30px; } </style> </head> <body> <div class="container-lg my-3"> <!-- Trigger Button HTML --> <input type="button" class="btn btn-primary" value="Toggle Button"> <!-- Collapsible Element HTML --> <div id="myCollapsible" class="collapse show"><p>This is a simple example of expanding and collapsing individual element via JavaScript. <br>Click on the <b>Toggle Button</b> button to see the effect.</p></div> </div> </body> </html>