Arpan Jain
Arpan Jain

Reputation: 146

Javascript modal pass dynamic variable in popup depending on dynamic id?

    <a id="wishlisticon" data-toggle="modal" data-target="#wishlist-modal" data-productid="1"></a>
    <a id="wishlisticon" data-toggle="modal" data-target="#wishlist-modal" data-productid="2"></a>
    <a id="wishlisticon" data-toggle="modal" data-target="#wishlist-modal" data-productid="3"></a>
    <a id="wishlisticon" data-toggle="modal" data-target="#wishlist-modal" data-productid="4"></a>

<script>
    $('#wishlist-modal').on('shown.bs.modal', function() {
       var $el = $("#wishlisticon");
       var $username = $el.data('productid');
       alert($username);
    });
</script>

There are several buttons on a page pointing to the same modal, however different data needs to be passed for each modal. The data to be passed is saved in data-productid. When the popup modal opens, I want to use data-productid variable.

The function is working but each time the alert is giving the same productid value (i.e = 1) The function is not able to understand that it should pickup the corresponding data-productid

Upvotes: 1

Views: 3664

Answers (1)

palaѕн
palaѕн

Reputation: 73966

As mentioned in the documentation, you can use event.relatedTarget to vary the contents of the modal depending on which button was clicked.

$('#wishlist-modal').on('shown.bs.modal', function(event) {
   var $el = $(event.relatedTarget);
   var $username = $el.data('productid');
   alert($username);
});

DEMO:

$('#exampleModal').on('shown.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('whatever')
  alert(recipient)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" >
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="1">Open modal for 1</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="2">Open modal for 2t</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="3">Open modal for 3</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">New message</h4>
      </div>
      <div class="modal-body">
        <h2>Hello World!</h2>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions