Maha
Maha

Reputation: 51

I am trying to add a modal to my html page but the close button seems to have a problem

I made a modal in my web-page and it shows when I click the but when I click close nothing happens (it doesn't close) any idea?

function voice() {
  $("#myModal").modal('show');
  $('.try').css("opacity", "0.3");

}

function closeModal() {
  $("#myModal").modal('hide');
  $('.try').css("opacity", "1");
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<a class="boxed-btn3 " onclick="voice()">----- Try -----</a>
<div id="myModal" class="modal fade" tabindex="-1" style="opacity:1;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h3 class="modal-title">Try saying something...</h3>
        <button type="button" class="close" data-dismiss="modal" onclick="closeModal()">&times;</button>

      </div>
      <div class="modal-body">
        <i class="fas fa-microphone"></i>
        <button class="button button5"></button>
      </div>

    </div>
  </div>
</div>

Upvotes: 0

Views: 55

Answers (2)

Maha
Maha

Reputation: 51

I solved it by changing this

<div id="myModal" class="modal fade" tabindex="-1" style="opacity:1;">

to this

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="false">

Upvotes: 1

Fareed Khan
Fareed Khan

Reputation: 2923

Your HTML must have correct order to make it work in your PC!

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <title>Hello, world!</title>
</head>

<body>

  <a class="boxed-btn3 " onclick="voice()">----- Try -----</a>
  <div id="myModal" class="modal fade" tabindex="-1" style="opacity:1;">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h3 class="modal-title">Try saying something...</h3>
          <button type="button" class="close" data-dismiss="modal" onclick="closeModal()">&times;</button>

        </div>
        <div class="modal-body">
          <i class="fas fa-microphone"></i>
          <button class="button button5"></button>
        </div>

      </div>
    </div>
  </div>

  <script>
    function voice() {
      $("#myModal").modal('show');
      $('.try').css("opacity", "0.3");

    }

    function closeModal() {
      $("#myModal").modal('hide');
      $('.try').css("opacity", "1");
    }
  </script>

  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
  </script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
  </script>
</body>

</html>

Upvotes: 0

Related Questions