I've read several similar questions on SO regarding Bootstrap's fade
classes, but none of them seem to address my issue.
I understand that the fade animation in Bootstrap 3 works by switching opacity
between 0
and 1
when you add the .in
class to an element that already has a .fade
class. This works fine for me when dealing with an element that already exists in the HTML (for example, this JS Fiddle demo).
The problem is that the transition doesn't seem to happen when the element is created dynamically with Javascript. For example, the following code will create and display an alert, but the CSS transition doesn't happen (tested in both Chrome and Firefox). If I manually add and remove the in
class in Dev Tools, the fade works fine:
$('<div class="alert fade">This is a test</div>')
.appendTo($someElement)
.addClass('in');
Clearly the .addClass()
function is working because the alert gets the in
class and is displayed, but the transition doesn't happen. Any idea how I can get it to work?
For an editable demo, see this pen on CodePen.