Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • BS5 Tutorial
  • BS5 Interview Questions
  • BS5 Layout
  • BS5 Content
  • BS5 Components
  • BS5 Helpers
  • BS5 Utilities
  • BS4 Tutorial
  • BS Tutorial
  • Bootstrap Cheatsheet
  • Tailwind
  • CSS Frameworks
  • HTML Formatter
Open In App
Next Article:
Bootstrap 5 Alerts JavaScript behavior Events
Next article icon

Bootstrap 5 Alerts JavaScript Behavior Methods

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Bootstrap 5 Alerts methods that are used to control the visibility of the alert component. For example, these methods can be used to close or dispose of an alert component. It is useful for displaying popup messages or user action notifications on a webpage.

Bootstrap 5 Alerts Methods

  • close(): It is used to close and hide the alert by removing it from the DOM.
  • dispose(): It is used to destroy the element as a Bootstrap 5 alert element.
  • getInstance(): It is a static method that is used to get the alert element associated with the DOM.
  • getOrCreateInstance(): It is a static method that is used to get the alert element associated with the DOM or create a new one if it is not present.

Syntax:

alertElement.alert_method()

Example 1: In this example, we will close an alert component using the "close" method given by the Bootstrap 5 alert object.

HTML
<!DOCTYPE html> <html lang="en">    <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">      <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"            rel="stylesheet"           integrity= "sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"            crossorigin="anonymous">     <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"              integrity= "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"             crossorigin="anonymous">     </script> </head>    <body class="m-2">     <h1 class="text-success">GeeksforGeeks</h1>     <div class="alert alert-success" id="alert">         A GFG Alert!     </div>      <button id="closeBtn">Close GFG Alert</button>      <script>         const alert = document.getElementById('alert')         const alertEl = new bootstrap.Alert(alert)          const closeBtn = document.getElementById('closeBtn')          closeBtn.addEventListener('click', function() {           alertEl.close()         })     </script> </body>    </html> 

Output:

bootstrapImprove1
Bootstrap 5 Alerts JavaScript Behavior Methods

Example 2: In this example, we will dispose of the alert element using the "dispose" method given by the Bootstrap 5 alert object. Once we dispose of an alert element, we cannot use any methods given by the Bootstrap 5 alert object as it gets destroyed in the DOM.

HTML
<!DOCTYPE html> <html lang="en">    <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1.0">      <link href= "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"            rel="stylesheet"           integrity= "sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"            crossorigin="anonymous">     <script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"              integrity= "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"             crossorigin="anonymous">     </script> </head>    <body class="m-2">     <h1 class="text-success">GeeksforGeeks</h1>     <h3>Bootstrap 5 Alert Methods</h3>     <div class="alert alert-success" id="alert">         A GFG Alert!     </div>      <button id="closeBtn">Close GFG Alert</button>     <button id="disposeBtn">Dispose GFG Alert</button>      <script>         const alert = document.getElementById('alert')         const alertEl = new bootstrap.Alert(alert)          const closeBtn = document.getElementById('closeBtn')         const disposeBtn = document.getElementById('disposeBtn')          closeBtn.addEventListener('click', function() {           alertEl.close()         })          disposeBtn.addEventListener('click', function() {           alertEl.dispose()           console.log('alert disposed!');         })     </script> </body>    </html> 

Output:

bootstrapImprove2
Bootstrap 5 Alerts JavaScript Behavior Methods

Reference: https://getbootstrap.com/docs/5.0/components/alerts/#methods


Next Article
Bootstrap 5 Alerts JavaScript behavior Events

D

dishebhbhayana
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • Bootstrap
  • Technical Scripter 2022
  • Bootstrap-5

Similar Reads

  • Bootstrap 5 Alerts Link color
    Bootstrap 5 Alerts Link Color class is used to set the color of the alert element. There are different kind of alerts that indicates different things, if there is a success then the color of the alert will be green, if it's a failure then it should show a red color alert. Each color of the alert rep
    3 min read
  • Bootstrap 5 Alerts Additional Content
    Bootstrap Alerts help inform users about particular events or give acknowledgment. But most of the time, only simple text alerts aren't enough to convey the message. There needs to be some additional content with a proper hierarchy. In such cases, Additional Content comes into the picture. It allows
    2 min read
  • Bootstrap 5 Alerts Icons
    Bootstrap 5 Alerts Icons are used to add icons to the alert bar to format the bar and make it more informative. To include the icon using <svg> tag we have to define its path inside the <path> tag in svg tag. We also need to specify its height and width to adjust its size according to th
    2 min read
  • Bootstrap 5 Alerts Dismissing
    Bootstrap 5 Alerts Dismissing provides a feature to dismiss the alerts inline and to enable alerts dismissing we need to add the .alert-dismissible class to the alert, and the data-bs-dismiss="alert" attribute to the close button. Syntax: <div class="alert alert-dismissible"> <button type="
    2 min read
  • Bootstrap 5 Alerts SASS
    Bootstrap 5 Alerts SASS can be used to change the default values provided for the alert by customizing the SCSS file. SASS variables of Alerts: $alert-padding-y: This variable provides the top and bottom padding of the alert. By default, it is 1rem.$alert-padding-x: This variable provides the left a
    5 min read
  • Bootstrap 5 Alerts JavaScript behavior
    Bootstrap 5 Alerts JavaScript behavior provides to enable the dismissal of an alert when an action is triggered. In addition to this, alert instances can be created with the help of the alert constructor. Although, there are a few events that enable the alert functionality by implementing Bootstrap’
    2 min read
  • Bootstrap 5 Alerts JavaScript behavior Triggers
    Bootstrap 5 Alert Triggers are used to close an alert. An alert can be closed using a close button inside the alert or by using a button outside the alert with a data-bs-target attribute. It should be noted that closing the alert also removes it from the DOM. Bootstrap 5 Alert Triggers used attribut
    2 min read
  • Bootstrap 5 Alerts JavaScript Behavior Methods
    Bootstrap 5 Alerts methods that are used to control the visibility of the alert component. For example, these methods can be used to close or dispose of an alert component. It is useful for displaying popup messages or user action notifications on a webpage. Bootstrap 5 Alerts Methodsclose(): It is
    2 min read
  • Bootstrap 5 Alerts JavaScript behavior Events
    In this article, we will learn about the Alerts JavaScript behavior Events fired when interacting with Bootstrap 5 Alerts. Bootstrap 5 Alert component is useful for providing alerts or feedback messages arising from the user interacting with the functionalities of the application. Bootstrap 5 Alerts
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences