Search code examples
jqueryhtmlcssbootstrap-modalsimplemodal

Creating modal in bootstrap


I have a website running on Bootstrap, JQuery, HTML, CSS. I want to add a modal output on my website such that when a user clicks on a hyperlink a modal opens and prints content of a txt file. E.g. I have two text files which contains different details. And my website consists of sentence --> This is sam. This is his website. I want that when a user clicks on sam or website the respective text file is opened in a simple modal.


Solution

  • Use bootstrap-modal and instead use button use a:

      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    
    <div class="container">
     
     
      <a href="#"  data-toggle="modal" data-target="#myModal">
        Open modal from a
      </a>
    
      <!-- The Modal -->
      <div class="modal" id="myModal">
        <div class="modal-dialog">
          <div class="modal-content">
          
            <!-- Modal Header -->
            <div class="modal-header">
              <h4 class="modal-title">Modal Heading</h4>
              <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            
            <!-- Modal body -->
            <div class="modal-body">
              Modal body..
            </div>
            
            <!-- Modal footer -->
            <div class="modal-footer">
              <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            </div>
            
          </div>
        </div>
      </div>
      
    </div>
    Edit to you comment

    is there any way I could output the content of text file in that modal? E.g. to view http://humanstxt.org/humans.txt in the modal? Use:

    $.get( "http://humanstxt.org/humans.txt", function( data ) {
      $('.modal-body').html= data;
    });
    

    Or with .ajax:

    $(document).ready(function(){
            $.ajax({url: "http://humanstxt.org/humans.txt", success: function(result){
                $('.modal-body').html(result);
            }});
    });