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: Cant show alert generated at runtime

pascalschneider free asked 3 years ago


Hello Community,

I've copied the following code (look up the resources at #1) from an example in the mdb alert generator. Which by itself works perfectly fine when I write it directly into my html source.

What i am trying to do is to generate the code for this dynamicly (id, message, alert level...) at server side and then just fill the html into the body (and show the alert). Up until then there are no problems but apparently when I try to call my new alert with the following code (look up resources at #2) the response is always: "Uncaught TypeError: mdb.Alert.getInstance(...) is null" When i add the same code that has been previously inserted at runtime directly into my source file it works.

Is there a way around this/ What am i doing wrong? Can you reproduce this type of behavior?

Thank you and Regards

Resources (screenshots, code snippets etc.)

1

<div class="alert alert-primary mb-0 alert-dismissible alert-absolute fade show " id="js-alert-1697070908" role="alert" data-mdb-color="secondary">
<i class="fas fa-check me-2"></i>
 Hello! I am a simple alert
<button type="button" class="btn-close ms-2" data-mdb-dismiss="alert" aria-label="Close"></button>
</div>

2

mdb.Alert.getInstance(document.getElementById('js-alert-1697070908')).show()

Grzegorz Bujański staff commented 3 years ago

Please try @softcon solution. It looks like you trying to get the element that doesn't exist on the page. Probably the code executes before the elements on the page load.


softcon premium answered 3 years ago


Have you tried wrapping it in an onload function?

window.onload = function(e){ 
    mdb.Alert.getInstance(document.getElementById('js-alert-1697070908')).show();
}

pascalschneider free commented 3 years ago

Hello, thank you for your response.

Technically the site is already fully loaded at the time, when i add the alert to my page. So waiting for the page to load results in the code not executing at all. I've tried printing the response of document.getElement... to console before calling the alert and it returned the new element just fine, so it should already be fully registered in my opinion. But I think i'll change my approach to this compleatly as this problem eats to much time right now.


FREE CONSULTATION

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

Status

Closed

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB Standard
  • MDB Version: 3.2.0
  • Device: PC
  • Browser: Tested with Chrome and Firefox
  • OS: Win 10
  • Provided sample code: No
  • Provided link: No
Tags