0

I found the script I wanted (I am a beginner with JS) : https://stackoverflow.com/a/25060114/4857932

Link HTML

<a href="#my_modal" data-toggle="modal" data-book-id="my_id_value">Open Modal</a>

Modal JavaScript

//triggered when modal is about to be shown
$('#my_modal').on('show.bs.modal', function(e) {

//get data-id attribute of the clicked element
var bookId = $(e.relatedTarget).data('book-id');

//populate the textbox
$(e.currentTarget).find('input[name="bookId"]').val(bookId);
});

But I would like to know how to open to modal without using the link. Only using JS. Is that possible ? I tried this code but it's not working :$('#myModal').modal('show'); Can you help me ?

Thanks for your help ! :)

Community
  • 1
  • 1
roillion
  • 23
  • 5

1 Answers1

0

First of all you have to say that you are using the Twitter Bootstrap framework. You have to use the .modal - method on the modal content::

$('#my_modal_content').modal('show');
$('#my_modal_content').on('shown.bs.modal', function() {
  //get data-id attribute of the clicked element
  var bookId = $(e.relatedTarget).data('book-id');
  //populate the textbox
  $(e.currentTarget).find('input[name="bookId"]').val(bookId);
})
Torsten Barthel
  • 3,059
  • 1
  • 26
  • 22