Dante Cervantes
Dante Cervantes

Reputation: 313

Change bootstrap modal title created on the fly

I have a bootstrap modal created on the fly like this

var popupTemplate = '
<div class="modal fade">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title" id="dropzoneModalTitle"></h4>
      </div>
      <div class="modal-body dropBox" id="dropzoneModalBody"></div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Save</button>
        <button type="button" class="btn btn-link" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>
';

then with a onClick listener with jquery i create a modal with that template like this

$(button).click(function(){
   $(popupTemplate).modal()
});

i want to change the title of that modal created on the fly, but is not working with this

var modal = $(popupTemplate).modal();
modal.find('.modal-title').text('HELLO');

any help for this?

Upvotes: 3

Views: 12354

Answers (2)

user10038293
user10038293

Reputation:

Another way is to change it inside the show event. Just add an id attr to the root div and do the following:

 $('#modalId').on('show.bs.modal', function () {

        $('#dropzoneModalTitle').text('My New Modal Title');

  });

Upvotes: 6

dana
dana

Reputation: 18125

Try modifying the HTML before creating the modal:

var modal = $(popupTemplate);
modal.find('.modal-title').text('HELLO');
modal.modal();

Upvotes: 9

Related Questions