-5

I would like to use bootstrap's styled alerts instead of the default javascript alerts.

How can I trigger the bootstrap alert instead of the regular alert in the following code?

<button type="button" onclick="alert('my message')">Click Me!</button>
<div class="alert alert-primary" role="alert">
  my alternative message
</div>

I don't want to see the message inside the div constantly on the screen, only when I press the button.

Edit: the answer that I am looking for is: what should be the value of onclick attribute so that the message will appear ONLY after clicking the button.

GyRo
  • 2,586
  • 5
  • 30
  • 38
  • Did you try something? please show us. – Nir Tzezana Nov 14 '18 at 13:01
  • https://stackoverflow.com/questions/10082330/dynamically-create-bootstrap-alerts-box-through-javascript – Quentin Nov 14 '18 at 13:03
  • @NirTzezana - I tried several examples for instance this one: https://www.bootply.com/r4cwV5Jw35 but this for example doesn't include the trigger action. Also if I reomve the 'fade' class the message appear on the screen, so I don't understand what it the best practice here – GyRo Nov 14 '18 at 13:09
  • @Quentin - the link that you provided doesn't include bootstrap div of alert. and also I don't see how to trigger the alert message (meaning call: show the message) – GyRo Nov 14 '18 at 13:13
  • The docs seem to cover this, have you read them? https://getbootstrap.com/docs/4.0/components/modal/ What part didn't you understand? – Liam Nov 14 '18 at 13:20
  • Possible duplicate of [Dynamically create Bootstrap alerts box through JavaScript](https://stackoverflow.com/questions/10082330/dynamically-create-bootstrap-alerts-box-through-javascript) – jeporcher Nov 14 '18 at 13:21
  • 1
    Also you want a modal (I think). [An alert is just an inline element with a style](https://getbootstrap.com/docs/4.0/components/alerts/) – Liam Nov 14 '18 at 13:21
  • Just use a small modal: https://www.codeply.com/go/DXQUa5sIhc – Carol Skelly Nov 14 '18 at 13:32

2 Answers2

0

Here is a more generic Solution for you, where you could type your wanted message, too.

function alert(message) {
  document.getElementById('alert-msg').innerHTML = message; // set message text
  if (document.getElementById('alert').classList.contains('d-none')) document.getElementById('alert').classList.remove('d-none'); // Display alert-box
}

function closeAlert() {
  document.getElementById('alert').classList.add('d-none'); // hide alert-box
  document.getElementById('alert-msg').innerHTML = ''; // reset message
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<button type="button" class="btn btn-sm btn-primary" onclick="alert('my message');">Click Me!</button>
<div class="alert alert-primary clearfix d-none" role="alert" id="alert">
  <span id="alert-msg"></span><button type="button" class="btn btn-sm btn-primary float-right" onclick="closeAlert();">Close</button>
</div>

Otherwise you could easily use modal and set its message like i did here:

function setMessage(message) {
  document.getElementById('alert-message').innerHTML = message;
  // set your Message to the <span> inside your modal.
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" onclick="setMessage('My Message');">
  Click Me!
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <span id="alert-message"></span>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
jsadev.net
  • 2,800
  • 1
  • 16
  • 28
-1

this is a 2 part solution.

First you would need HTML markup that represent the alert (and the trigger button)

<button id="btnShowAlert" type="button">Click Me!</button>
<div id="myAlert" style="display: none;" class="alert alert-primary" role="alert">
  my alternative message
</div>

Then secondly you would need the JavaScript (jQuery) that listens for click events on the button, then show the alert accordingly.

$('#btnShowAlert').on('click', function(){
    $('#myAlert').style('display', 'block');
});

My question is... Are you perhaps looking for Modals ???