Holiday Notice: Support will be provided on a limited scale from December 24th, 2024, to January 2nd, 2025. Happy holidays and a wonderful New Year!


Topic: Basic alerts call by a function and automatically hide

ExTox priority asked 2 years ago


Hi everyone, this was my previous code before I used mdb. The principle is simple. I want to use a method to create different alerts which are triggered on an event. I would like to pass the content and the type to the alert.

Now I would like to create the same alert with the help of MDB which will be closed automatically.

HTML:

<div id="liveAlertPlaceholder" class="alert"></div>

JS-Code:

var alertPlaceholder = document.getElementById('liveAlertPlaceholder')

function alert(message, type)
{

    var wrapper = document.createElement('div')
        wrapper.innerHTML = '<div class="alert alert-' + type + ' alert-dismissible" role="alert">' + message + '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>'

        alertPlaceholder.append(wrapper);
}


$('#example-btn').click(function () {
        alert('This is an example text.', 'primary');
});

I had tried to get a simple alert with the help of the documentation, but it didn't work.

HTML: Primary

    <div class="alert fade"
         id="alert-primary"
         role="alert"
         data-mdb-color="primary"
         data-mdb-position="top-right"
         data-mdb-stacking="true"
         data-mdb-width="535px"
         data-mdb-append-to-body="true"
         data-mdb-hidden="true"
         data-mdb-autohide="true"
         data-mdb-delay="2000">
        A simple primary alert
    </div>

JS:

const triggers = [
    'primary',
    'secondary',
    'success',
    'danger',
    'warning',
    'info',
    'light',
    'dark',
];
const basicInstances = [
    'alert-primary',
    'alert-secondary',
    'alert-success',
    'alert-danger',
    'alert-warning',
    'alert-info',
    'alert-light',
    'alert-dark',
];

triggers.forEach((trigger, index) => {
    let basicInstance = mdb.Alert.getInstance(document.getElementById(basicInstances[index]));
    document.getElementById(trigger).addEventListener('click', () => {
        basicInstance.show();
    });
});

$("#primary").click(function () {
    mdb.Alert.getInstance(document.getElementById('alert-primary')).show();
});

Grzegorz Bujański staff answered 2 years ago


In the JS code, you added a click listener, but the HTML code lacks a button that will open an alert on a click. In addition, you left the code responsible for assigning events and creating an instance for other alerts - note that there are several buttons in the documentation and they open various alerts. My guess is that you have an error in the console and therefore it is not working properly. Here you will find a snippet in which I created a working example using your code: https://mdbootstrap.com/snippets/standard/grzegorz-bujanski/3921176#html-tab-view



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB Standard
  • MDB Version: MDB5 4.0.0
  • Device: HP
  • Browser: Chrome
  • OS: Windows
  • Provided sample code: No
  • Provided link: No