0

I'm following a tutorial using Bootstrap 3. So far so good, except for a non-expected behavior: when I click a button associated with a jQuery function, the alert gets displayed fine. However, once I dismiss it, clicking the button again doesn't do anything, that is, looks like it's a no-op.

For brevity, I'll include the relevant sections. I have the following <div>:

    <div class="row" id="bigCallout">
        <div class="col-12">

            <div class="alert alert-success alert-block fade in" id="successAlert">
                <button type="button" class="close" data-dismiss="alert">&times;</button>
                <h4>Success!</h4>
                <p>You just made this element display using jQuery.</p>
            </div>

        </div>
    </div> <!-- end bigCallout -->

Below I include my JS file:

<!-- Custom JS -->
<script src="includes/js/script.js"></script>

The JS file mentioned above contains this only function:

$(function() {

   $('#alertMe').click(function(e) {

       e.preventDefault();

       $('#successAlert').slideDown();

   });

});

Why is the function being executed only the first time the button is clicked? Thanks!

2
  • And no error in the Dev console? Commented Nov 26, 2014 at 3:34
  • No error. Please see Jason's response, which explains why it was behaving the way it was. Commented Nov 26, 2014 at 4:43

1 Answer 1

1

When you rely on the data-dismiss="alert" which dismisses/closes the alert, the alert is actually removed from the data model. Therefore, when you click to attempt to slideDown the element again, that element no longer exists.

To get the desired behavior to re-use an alert message that has been "dismissed", you'll have to remove your data-dismiss and respond to the click event on the close button to slideUp or hide the element. Then your subsequent slideDown code will find the DOM elements successfully.

[EDIT] Here is a JSFiddle: http://jsfiddle.net/de4qkoe5/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.