Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make modal visible by default when html page runs

I am creating a pop up div (modal) which will appear on screen by default. However I succeed to open it on button click but I want it open by default whenever I run the HTML page. Here is my code.

<!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="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    </head>
    <body>

        <div class="container">
            <h2>Modal Example</h2>
            <!-- Trigger the modal with a button -->
            <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">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>

Where I am showing the modal using a button.In that I am using data-target property to invoke the modal. So how to do it without any click event?

like image 525
Dhiraj Avatar asked Sep 18 '15 09:09

Dhiraj


People also ask

How do you show a modal in HTML?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do you prevent Bootstrap modal from hiding when clicked outside the content area?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.

How do I stop the modal pop up in HTML?

// We use data-dismiss property of modal-up in html to close the modal-up,such as <div class='modal-footer'><button type='button' class="btn btn-default" data-dismiss='modal'>Close</button></div> // We can close the modal pop-up through java script, such as <div class='modal fade pageModal' id='openModal' tabindex='-1' ...

How do you display a modal in JavaScript?

In other to show the modal, create a function called openModal . Inside this function, you'll use the DOM classList property which takes in different methods like . remove() and . add() to remove the hidden class from the modal and overlay .


2 Answers

Use .modal('show') to show the model manually

$(document).ready(function() {
  $('#myModal').modal('show');
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">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>
like image 167
Pranav C Balan Avatar answered Oct 10 '22 10:10

Pranav C Balan


Add class in to html code , such that

<div class="modal fade show d-block" id="newModal" role="dialog">

or you can call the following function when the page loads

$('#newModal').modal('show');
like image 35
mhdcindioglu Avatar answered Oct 10 '22 09:10

mhdcindioglu