Rits
Rits

Reputation: 47

Unable to load Bootstrap Modal via Ajax

I have my html code as:

<button class="btn btn-default" id="openForm">
        New           
</button>

<!-- Modal -->
<div class="modal fade hide" id="formModal">
  <div class="modal-header">
    <h4>
        Form
    </h4>
  </div>

  <div class="modal-body" id="formModalBody">
  </div>

  <div class="modal-footer">
    <a href="#" data-dismiss="modal" class="btn">Close</a>        
  </div>
</div>

My Java script reads as:

$(document).on('click', '#openForm', function(e) {
e.preventDefault();

$("#formModalBody").html("");
$("#formModal").modal('show');

$.post('index.php/customers/view/-1/',
        {id: -1},
        function (html) {
            $("#formModalBody").html(html);
        }
    );
});

I am using Twitter Bootstrap & Codeignitor. The post url above is index.php/Controller/function

When I am clicking on the button; I am not able to see the Bootstrap Modal. The page fades in, but there is no modal visible.

Upvotes: 1

Views: 650

Answers (2)

Girish
Girish

Reputation: 12127

You should use anchor link instead of button, it will work default settings

 <a href="/model_link" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Launch demo modal
    </a>

The href link response code will append in

<div class="modal-body" id="formModalBody">
<!-- modal body -->
</div>

Upvotes: 0

Dreathlord
Dreathlord

Reputation: 46

this is for bootstrap 3 and can be triggered by html alone without javascript it will run Launch demo modal

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body" id="formModalBody">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

<!-- For javascript -->
$.post('index.php/customers/view/-1/',
        {id: -1},
        function (html) {
            $("#formModalBody").html(html);
        }
);

Upvotes: 1

Related Questions