Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alerts added with JS cannot be dismissed #60

Closed
ghost opened this issue Jun 22, 2016 · 11 comments
Closed

Alerts added with JS cannot be dismissed #60

ghost opened this issue Jun 22, 2016 · 11 comments
Labels
duplicate known issue

Comments

@ghost
Copy link

ghost commented Jun 22, 2016

Alerts added to the document using DOM operations in JS cannot be dismissed. A testcase is attached below:

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
  <div id="user-messages" class="container col-sm-12">
   <!-- <div class="alert alert-danger fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">&times;</a>
    {message 1}
   </div> -->
  </div>
  <script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.native/1.0.2/bootstrap-native.min.js"></script>
  <script type="text/javascript">
  function showMessage (html_msg) {
    var a = document.createElement("div");
    a.classList.add("alert", "alert-danger", "fade", "in");
    a.innerHTML = '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
    a.innerHTML += html_msg;
    var x = document.getElementById("user-messages");
    x.appendChild(a);
  }
  showMessage("{message 2}");
  </script>
</body>
</html>

In addition, and quite strangely in fact, if you remove the commented out section in the above testcase, the alerts can be dismissed for the user-messages div (but not for any other div).

@thednp
Copy link
Owner

thednp commented Jun 22, 2016

My demo showcases that later added alerts work properly, but please download the latest version and test (the CDN is not up to latest version yet) to make sure we're talking the same thing.

Also make sure you check the demo on the button scripting, there is an example with a properly handled later added alert that works perfect.

@thednp thednp closed this as completed Jun 22, 2016
@ghost
Copy link
Author

ghost commented Jun 22, 2016

@thednp It is also an issue in the latest version.

@thednp
Copy link
Owner

thednp commented Jun 22, 2016

OK then, please provide a test demo I can check.

@ghost
Copy link
Author

ghost commented Jun 22, 2016

@thednp Please check my first post, it contains a testcase and other details.

I initially didn't want to provide a JSFiddle since dismissible alerts do not work at all there at all, be it JS or non-JS ones but here it is anyway.

@thednp
Copy link
Owner

thednp commented Jun 22, 2016

jsfiddle doesn't work because it always adds js to the <head>. Here's your example on codepen.

As described in my demo/doc, you must always make sure to have all JS in your site's <body>, maybe that's your issue.

@ghost
Copy link
Author

ghost commented Jun 22, 2016

Apparently I've not been able to get the message across:

  • I'm trying to add a dismissible alert using JS.
  • When I try to do this in an div that doesn't have an alert already in it as part of the HTML source (e.g. <div id="user-messages"></div>, the alert that's added with JS can't be dismissed.
  • As a workaround to this, I can put a preexisting alert, (i.e. <div id="user-messages"><div class="alert alert-info"><a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">&times;</a></div></div> but this workaround shouldn't be needed, right?

@thednp
Copy link
Owner

thednp commented Jun 22, 2016

Of course not. The Alert component can handle later added alerts on the fly as long as you have the right JS at the right place.

@ghost
Copy link
Author

ghost commented Jun 22, 2016

But then it's not working as intended! Please take the above file and try running it in a browser, first as is (i.e. no preexisting alert) and then with the commented out section enabled (i.e. with a preexisting alert), I hope you will understand what this issue is all about.

@thednp
Copy link
Owner

thednp commented Jun 22, 2016

It seems Alert does not work in that case, when no alert component found and initialized before your code, any other later added alerts won't work.

But here's an easy fix, when you append the alert to your wrapper, also initialize it, here's the fixed example. I added new Alert(a); to initialize after the append.

@ghost
Copy link
Author

ghost commented Jun 23, 2016

Thank you for looking into this and providing a workaround.

However, are there any plans to resolve this issue in bootstrap.native itself?

@thednp
Copy link
Owner

thednp commented Jun 23, 2016

What I gave you isn't a workaround, it's the right way to do.

@thednp thednp mentioned this issue Jun 27, 2016
@thednp thednp added the duplicate known issue label Aug 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate known issue
Projects
None yet
Development

No branches or pull requests

1 participant