Home > Net >  Bootstrap modal does not show with microsoft visual studio
Bootstrap modal does not show with microsoft visual studio

Time:08-26

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 >
        <h2>Activate Modal with JavaScript</h2>
        <!-- Trigger the modal with a button -->
        <button type="button"  id="myBtn">Open Modal</button>

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

                <!-- Modal content-->
                <div >
                    <div >
                        <button type="button"  data-dismiss="modal">&times;</button>
                        <h4 >Modal Header</h4>
                    </div>
                    <div >
                        <p>Some text in the modal.</p>
                    </div>
                    <div >
                        <button type="button"  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 ?

CodePudding user response:

try this

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

CodePudding user response:

Try to use bootstrap custom attribute to trigger the modal like below:

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

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

CodePudding user response:

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 >
      <h2>Activate Modal with JavaScript</h2>
      <!-- Trigger the modal with a button -->
      <button
        type="button"
        
        data-toggle="modal"
        data-target="#myModal"
        id="myBtn"
      >
        Open Modal
      </button>

      <!-- Modal -->
      <div  id="myModal" role="dialog">
        <div >
          <!-- Modal content-->
          <div >
            <div >
              <button type="button"  data-dismiss="modal">
                &times;
              </button>
              <h4 >Modal Header</h4>
            </div>
            <div >
              <p>Some text in the modal.</p>
            </div>
            <div >
              <button
                type="button"
                
                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 >
      <h2>Activate Modal with JavaScript</h2>
      <!-- Trigger the modal with a button -->
      <button
        type="button"
        
        id="myBtn"
      >
        Open Modal
      </button>

      <!-- Modal -->
      <div  id="myModal" role="dialog">
        <div >
          <!-- Modal content-->
          <div >
            <div >
              <button type="button"  data-dismiss="modal">
                &times;
              </button>
              <h4 >Modal Header</h4>
            </div>
            <div >
              <p>Some text in the modal.</p>
            </div>
            <div >
              <button
                type="button"
                
                data-dismiss="modal"
              >
                Close
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <script>
      $(document).ready(function(){ $("#myBtn").click(function(){ $("#myModal").modal('show'); }); });
    </script>
  </body>
</html>

CodePudding user response:

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 >
        <h2>Activate Modal with JavaScript</h2>
        <!-- Trigger the modal with a button -->
        <button type="button"
                
                id="myBtn">
            Open Modal
        </button>

        <!-- Modal -->
        <div  id="myModal" role="dialog">
            <div >
                <!-- Modal content-->
                <div >
                    <div >
                        <button type="button"  data-dismiss="modal">
                            &times;
                        </button>
                        <h4 >Modal Header</h4>
                    </div>
                    <div >
                        <p>Some text in the modal.</p>
                    </div>
                    <div >
                        <button type="button"
                                
                                data-dismiss="modal">
                            Close
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

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