how to use addEventListener with bootstrap5 ?
- User
how to use addEventListener with bootstrap5 ?
- Assistant
To use addEventListener with Bootstrap5, you can follow these steps:
-
Identify the element or component in your HTML code that you want to attach the event listener to.
-
In your JavaScript code, create a reference to that element or component using document.querySelector() or similar method.
-
Use the .addEventListener() method to attach the appropriate event listener to the element or component.
-
Within the event listener, write the code that you want to execute when the event is triggered.
Here is an example of adding an event listener to a button:
HTML code:
code<button id="myButton" class="btn btn-primary">Click me!</button>JavaScript code:
code// Get button element const myButton = document.querySelector('#myButton'); // Add event listener to button myButton.addEventListener('click', function() { // Code to execute when button is clicked alert('Button clicked!'); }); -
- User
I want to use Bootstrap Modal
- Assistant
To use Bootstrap Modal with addEventListener, you can follow these steps: