display alert after button is clicked

Test Account

I have a button that pops up an alert "Hello" after being clicked. It works fine for the first click but after I close the alert and I clicked the button again, the alert doesn't shows. Please help.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.hidden {
	display: none;
}
</style>
<title>Show Alert</title>
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
	<div class="container">

		<div class="row">
			<div class="alert alert-success hidden" id="success-alert">
				<button type="button" class="close" data-dismiss="alert">x</button>
				<span class="glyphicon glyphicon-ok"></span> Hello
			</div>
		</div>

		<div class="row">
			<button id="btn" type="button" class="btn btn-default">Open
				Alert</button>
		</div>
	</div>

	<script type="text/javascript">
		$(document).ready(function() {
			$("#btn").click(function() {
				$('#success-alert').removeClass('hidden');
			});

		});
	</script>
</body>

</html>

ab29007

Your hidden class should come back but is not due to data-dismiss. Remove data-dismiss from your closing button and attach a click event to it which will give back the hidden class on click. Here's the working code.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
	src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script
	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.hidden {
	display: none;
}
</style>
<title>Show Alert</title>
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
	<div class="container">

		<div class="row">
			<div class="alert alert-success hidden" id="success-alert">
				<button type="button" class="close">x</button>
				<span class="glyphicon glyphicon-ok"></span> Hello
			</div>
		</div>

		<div class="row">
			<button id="btn" type="button" class="btn btn-default">Open
				Alert</button>
		</div>
	</div>

	<script type="text/javascript">
		$(document).ready(function() {
			$("#btn").click(function() {
				$('#success-alert').removeClass('hidden');
			});
          $("button.close").click(function() {
				$('#success-alert').addClass('hidden');
			});

		});
	</script>
</body>

</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to display an alert dialog (with ok button) when an image button is clicked?

From Dev

How to display the selections in an alert box when the button is clicked? (with Javascript)

From Dev

how to display information in panel after a button is clicked?

From Dev

Android: Display a toast message after a custom time when a button is clicked

From Dev

Display text after submit button is clicked. PHP

From Dev

Android: Display a toast message after a custom time when a button is clicked

From Dev

Why does Mozilla Firefox page refresh after javascript alert button clicked?

From Dev

Disable button after clicked

From Dev

jQuery event with Alert "OK" button clicked

From Dev

When button is clicked the Alert Dialog does not appear

From Dev

jQuery event with Alert "OK" button clicked

From Dev

How to create alert dialog on button when it is clicked?

From Dev

Display a frame when a button is clicked

From Dev

Display alert after conditions are met

From Dev

Display alert after conditions are met

From Dev

Display Content of Button When Button was Clicked

From Dev

Display elements after one is clicked

From Dev

Button color after being clicked

From Dev

Disable button after clicked PHP

From Dev

Showing a photo after a button is clicked

From Dev

WRITE statement after button was clicked

From Dev

How to display a hidden part right underneath the submit button after it's clicked?

From Dev

How to display a hidden part right underneath the submit button after it's clicked?

From Dev

How to get radio button to display alert message

From Dev

Display Alert Message: On OK button click RedirectToAction

From Dev

How can I display an alert with the position of the element that was clicked?

From Dev

Alert Dialog show when clicked on drop down list in Button

From Dev

Why is this alert box not popping up when button clicked?

From Dev

open alert window once modal submit button clicked

Related Related

  1. 1

    How to display an alert dialog (with ok button) when an image button is clicked?

  2. 2

    How to display the selections in an alert box when the button is clicked? (with Javascript)

  3. 3

    how to display information in panel after a button is clicked?

  4. 4

    Android: Display a toast message after a custom time when a button is clicked

  5. 5

    Display text after submit button is clicked. PHP

  6. 6

    Android: Display a toast message after a custom time when a button is clicked

  7. 7

    Why does Mozilla Firefox page refresh after javascript alert button clicked?

  8. 8

    Disable button after clicked

  9. 9

    jQuery event with Alert "OK" button clicked

  10. 10

    When button is clicked the Alert Dialog does not appear

  11. 11

    jQuery event with Alert "OK" button clicked

  12. 12

    How to create alert dialog on button when it is clicked?

  13. 13

    Display a frame when a button is clicked

  14. 14

    Display alert after conditions are met

  15. 15

    Display alert after conditions are met

  16. 16

    Display Content of Button When Button was Clicked

  17. 17

    Display elements after one is clicked

  18. 18

    Button color after being clicked

  19. 19

    Disable button after clicked PHP

  20. 20

    Showing a photo after a button is clicked

  21. 21

    WRITE statement after button was clicked

  22. 22

    How to display a hidden part right underneath the submit button after it's clicked?

  23. 23

    How to display a hidden part right underneath the submit button after it's clicked?

  24. 24

    How to get radio button to display alert message

  25. 25

    Display Alert Message: On OK button click RedirectToAction

  26. 26

    How can I display an alert with the position of the element that was clicked?

  27. 27

    Alert Dialog show when clicked on drop down list in Button

  28. 28

    Why is this alert box not popping up when button clicked?

  29. 29

    open alert window once modal submit button clicked

HotTag

Archive