Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

Bootstrap 5.0.2 Offcanvas - Show with jQuery or JavaScript

Hi E's,

I'm having some difficulties in understanding how Bootstrap's Offcanvas works with jQuery or JavaScript, and so I ask for your help.


The objective is show Offcanvas when I click in the button, and I try this code but the code doesn't work:

<!doctype html>
<html lang="pt"> <head>     <meta charset="utf-8">     <meta name="viewport" content="width=device-width, initial-scale=1">     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">     <title>1751 htmlbs sem_script.php</title> </head> <body>     <a class="btn btn-primary" role="button" id="bt">         Open Offcanvas     </a>     <div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">         <div class="offcanvas-header">             <h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas</h5>             <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>         </div>         <div class="offcanvas-body">         </div>     </div>     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>     <script>         var offcanvasElementList = [].slice.call(document.querySelectorAll('.offcanvas'))         var offcanvasList = offcanvasElementList.map(function(offcanvasEl) {             return new bootstrap.Offcanvas(offcanvasEl)         });         $("#bt").click(function() {             $("#offcanvasExample").show();         });     </script> </body> </html>

Open in new window

What is wrong?


The best regards,

JC

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

You don't need your javascript, just change the button to

<a class="btn btn-primary" data-bs-toggle="offcanvas" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample" aria-controls="offcanvasExample"id="bt">
        Open Offcanvas
    </a>

Open in new window

Avatar of Pedro Chagas

ASKER

Hi Scott,
I need a solution in jQuery or JavaScript.
I have some parameters to pass, and depending on this information, the Offcanvas content is variable.

~JC
ASKER CERTIFIED SOLUTION
Avatar of Pedro Chagas
Pedro Chagas
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Scott.