5

I use Boostrap 3.7 and Blade (Laravel 5.5).

I'm trying to display console.log('works') when my boostrap modal opens but it didn't work.

HTML :

@foreach(...)

    ...

    <div class="modal fade" id="reservationModal" tabindex="-1" role="dialog" aria-labelledby="reservationModal" aria-hidden="true">
        <div class="modal-dialog">
            ...
        </div>
    </div>
@endforeach

JS :

$('#reservationModal').on('shown.bs.modal', function (e) {
    console.log('works');
});

I followed this doc : https://getbootstrap.com/docs/3.3/javascript/#modals

And I already read that : Calling a function on bootstrap modal open

Thank's for help !

EDIT 1:

I solved the problem with this code :

$(document).on('show.bs.modal', '#reservationModal', function (e) {
    console.log('works');
});

But how to differenciate modals (because they are into foreach loop)?

Something like :

$(document).on('show.bs.modal', '#reservationModal-specificId', function (e) {
    console.log('works');
});
isherwood
  • 58,414
  • 16
  • 114
  • 157
Royce
  • 1,557
  • 5
  • 19
  • 44

2 Answers2

4

I think your event listeners are created before HTML printing. So try this code.

$(document).on('show.bs.modal', '#reservationModal', function (e) {
    console.log('works');
});

$(document).on('show.bs.modal', '#reservationModal', function (e) {});

the bold characters will help to identify your modal

ANSWER FOR YOUR UPDATED PART

run the loop and create your modal as follows

<div class="modal fade reservationModal" id="reservationModal1" tabindex="-1" role="dialog" aria-labelledby="reservationModal" aria-hidden="true">
    <div class="modal-dialog">
        ...
    </div>
</div>

<div class="modal fade reservationModal" id="reservationModal2" tabindex="-1" role="dialog" aria-labelledby="reservationModal" aria-hidden="true">
    <div class="modal-dialog">
        ...
    </div>
</div>
...... and so on

Give reservationModal as class

and id as an incremented value appended to it

$(document).on('show.bs.modal', '.reservationModal', function (e) {
    console.log($(this).attr("id"));
});
Faraz PV
  • 502
  • 2
  • 11
1

As pointed out by @Rory McCrossan in a comment, the duplication of the id is the key problem of your code. To fix this, you may use index:

id="reservationModal-{{$loop->index}}"

And use start with selector like this if you want to call on each modal:

$('[id^="reservationModal-"]').on('shown.bs.modal', function (e) {
    console.log('works');
});

Or, just use indexed selector to use on particular modal:

$('#reservationModal-3').on('shown.bs.modal', function (e) {
    console.log('works');
});
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • My objective is to have a generic code for each modal yes. But your solution with `$('[id^="reservationModal-"]')` and`{{$loop->index}}` seems didn't works because modal didn't open at all. – Royce Dec 22 '17 at 11:58
  • I think that the first solution, with `[id^="reservationModal-"]` is the right way but didn't works and i don't know why. – Royce Dec 22 '17 at 12:25