skip to Main Content

I have the following code copy-pasted from w3schools Modal training page .

    <!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=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.6.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

    <div class="container">
        <h2>Activate Modal with JavaScript</h2>
        <!-- Trigger the modal with a button -->
        <button type="button" class="btn btn-info btn-lg" id="myBtn">Open Modal</button>

        <!-- Modal -->
        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">

                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Modal Header</h4>
                    </div>
                    <div class="modal-body">
                        <p>Some text in the modal.</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <script>
        $(document).ready(function () { $("#myBtn").click(function () { $("#myModal").modal('show'); }); });
    </script>

</body>
</html>

I am using MS Visual studio 2019, with Jquery nuget package installed in the project. I simply copy the code to test it in the razor blank page, execute , then I get nothing apart from the button, when it is clicked , no modal appears.

Nevertheless, that code works for the online html compilers.

Am I missing something here ?

4

Answers


  1. Chosen as BEST ANSWER

    Problem solved :

    Just added :

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    

    Now the modal is show upon clicking the button.

    The code works on Visual studio and looks like this now :

        <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    </head>
    
    <body>
        <div class="container">
            <h2>Activate Modal with JavaScript</h2>
            <!-- Trigger the modal with a button -->
            <button type="button"
                    class="btn btn-info btn-lg"
                    id="myBtn">
                Open Modal
            </button>
    
            <!-- Modal -->
            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">
                                &times;
                            </button>
                            <h4 class="modal-title">Modal Header</h4>
                        </div>
                        <div class="modal-body">
                            <p>Some text in the modal.</p>
                        </div>
                        <div class="modal-footer">
                            <button type="button"
                                    class="btn btn-default"
                                    data-dismiss="modal">
                                Close
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
        <script>
            $(document).ready(function () { $("#myBtn").click(function () { $("#myModal").modal('show'); }); });
        </script>
    </body>
    </html>
    

  2. try this

    $(document).ready(function(){ $("#myBtn").click(function(){ $("#myModal").modal('show'); }); });
    
    Login or Signup to reply.
  3. Try to use bootstrap custom attribute to trigger the modal like below:

    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" id="myBtn">Open Modal</button>
    

    use data-toggle and data-target in button tag.

    Login or Signup to reply.
  4. I have edited your code. use data-toggle="modal" and data-target="#YOUR_MODAL_ID" in the button to make it work.

    Here YOUR_MODAL_ID=myModal

    Example using from HTML tag

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=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.6.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
      </head>
    
      <body>
        <div class="container">
          <h2>Activate Modal with JavaScript</h2>
          <!-- Trigger the modal with a button -->
          <button
            type="button"
            class="btn btn-info btn-lg"
            data-toggle="modal"
            data-target="#myModal"
            id="myBtn"
          >
            Open Modal
          </button>
    
          <!-- Modal -->
          <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
              <!-- Modal content-->
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal">
                    &times;
                  </button>
                  <h4 class="modal-title">Modal Header</h4>
                </div>
                <div class="modal-body">
                  <p>Some text in the modal.</p>
                </div>
                <div class="modal-footer">
                  <button
                    type="button"
                    class="btn btn-default"
                    data-dismiss="modal"
                  >
                    Close
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </body>
    </html>

    Example using with Javascript (jQuery)

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=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.6.0/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
      </head>
    
      <body>
        <div class="container">
          <h2>Activate Modal with JavaScript</h2>
          <!-- Trigger the modal with a button -->
          <button
            type="button"
            class="btn btn-info btn-lg"
            id="myBtn"
          >
            Open Modal
          </button>
    
          <!-- Modal -->
          <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
              <!-- Modal content-->
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal">
                    &times;
                  </button>
                  <h4 class="modal-title">Modal Header</h4>
                </div>
                <div class="modal-body">
                  <p>Some text in the modal.</p>
                </div>
                <div class="modal-footer">
                  <button
                    type="button"
                    class="btn btn-default"
                    data-dismiss="modal"
                  >
                    Close
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <script>
          $(document).ready(function(){ $("#myBtn").click(function(){ $("#myModal").modal('show'); }); });
        </script>
      </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search