TheMightyLlama
TheMightyLlama

Reputation: 1273

Unable to have multiple bootstrap modals on the same page

I'm completely stumped. I have separate separate triggers all very similar

<a style="margin: 4px 4px 4px 4px !important" 
   data-toggle="modal" 
   data-target="#video_modal" 
   data-title="YouTube video title" 
   data-youtube-url="YouTube/url/">Montenegro protesters decry opposition's use of Serbian symbols
</a>

<a style="margin: 4px 4px 4px 4px !important" 
   data-toggle="modal" 
   data-target="#audio_modal" 
   data-title="Soundcloud item title" 
   data-soundcloud-url="soundcloud/url">Katarina Panic: Montenegro elections-The real winners are citizens
</a>

The modals are as below:

<div class="modal fade bd-example-modal-lg" id="video_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item youtube-iframe" src=""></iframe>
      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>


<div class="modal fade" id="submission-modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
         <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item soundcloud-iframe" src=""></iframe>
        </div>
      </div>
    </div>
  </div>
</div>

And the javascript which populates those modals.

//SOUNDCLOUD MODAL SCRIPT
$('#audio_modal').on('show.bs.modal', function (event) {
  var source = $(event.relatedTarget); // Button that triggered the modal
  console.log(source + "Audio Modal JS");
  var title = source.data('title');
  var soundcloudurl = source.data('soundcloud-url'); // Extract info from data-* attributes

  //MODIFY THE SOUNDCLOUD URL TO INCLUDE THE EMBED PART OF THE URL STRING
  var iframeLink = soundcloudurl.replace(soundcloudrurl, 'https://w.soundcloud.com/player/?url=$1');

  //SET THE TEXT AND THE SRC ATTRIBUTE OF THE MODAL
  var modal = $(this);
  modal.find('.modal-title').text(title);
  modal.find('.audio-iframe').attr("src",iframeLink);

})

//YOUTUBE MODAL SCRIPT
$('#video_modal').on('show.bs.modal', function (event) {
  var source= $(event.relatedTarget); // Button that triggered the modal
  console.log(source + "Video Modal JS");
  var title = source.data('title');
  var youtube_url = source.data('youtube-url'); // Extract info from data-* attributes

  //MODIFY THE YOUTUBE URL TO INCLUDE THE EMBED PART OF THE URL STRING
  var iframeLink = youtube_url.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, 'www.youtube.com/embed/$1');

  //SET THE TEXT AND THE SRC ATTRIBUTE OF THE MODAL
  var modal = $(this);
  modal.find('.modal-title').text(title);
  modal.find('.youtube-iframe').attr("src",iframeLink);

})

Strangely, the YouTube modal appears and populates absolutely fine. However the sound-cloud modal isn't triggered at all.

I'm pretty sure I've got the classes and id's correct (because the YouTube modal works). But there's something I'm just not taking into account. Any suggestions?

Upvotes: 2

Views: 2670

Answers (2)

Always Helping
Always Helping

Reputation: 14570

You do not need two separate modals on your page at all. I have refactored your code to have one modal which works for multiple things.

You can assign an id to your a element and check which one triggered the modal by checking it attr and you can apply the data accordingly to that modal.

Ideally, less code is always better to have dynamic results.

Live Working Example:

//SOUNDCLOUD MODAL SCRIPT
$('#myModal').on('show.bs.modal', function(event) {

  var modal = $(this)
  var button = $(event.relatedTarget) // Button that triggered the modal  

  if (button.attr('id') == 'soundcloud') {
    var title = button.data('title');
    var soundcloudurl = button.data('soundcloud-url'); // Extract info from data-* attributes
    //MODIFY THE SOUNDCLOUD URL TO INCLUDE THE EMBED PART OF THE URL STRING
    var iframeLink = soundcloudurl.replace(soundcloudurl, 'https://w.soundcloud.com/player/?url=$1');

    //SET THE TEXT AND THE SRC ATTRIBUTE OF THE MODAL
    modal.find('.modal-title').text(title);
    modal.find('.iframe').attr("src", iframeLink);
  } else {
    var title = button.data('title');
    var youtube_url = button.data('youtube-url'); // Extract info from data-* attributes

    //MODIFY THE YOUTUBE URL TO INCLUDE THE EMBED PART OF THE URL STRING
    var iframeLink = youtube_url.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, 'www.youtube.com/embed/$1');

    //SET THE TEXT AND THE SRC ATTRIBUTE OF THE MODAL
    modal.find('.modal-title').text(title);
    modal.find('.iframe').attr("src", iframeLink);
  }
})
#youtube {
  background: green;
  cursor: pointer;
}

#soundcloud {
  background: Yellow;
  cursor: pointer;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<a id="youtube" style="margin: 4px 4px 4px 4px !important" data-toggle="modal" data-target="#myModal" data-title="YouTube video title" data-youtube-url="YouTube/url/">Youtube
</a>
<br>
<a id="soundcloud" style="margin: 4px 4px 4px 4px !important" data-toggle="modal" data-target="#myModal" data-title="Soundcloud item title" data-soundcloud-url="soundcloud/url">Sound Cloud
</a>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel1"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item iframe" src=""></iframe>
        </div>
      </div>
    </div>
  </div>
</div>

Upvotes: 2

blacksheep_trident
blacksheep_trident

Reputation: 312

I have tested your code in my local environment and the problem was with the id value of the second modal as which is submission-modal but should have been audio_modal as shown in below image enter image description here

Also there was a minor typo soundcloudrurl which should have been soundcloudurl enter image description here

The full code is below.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script
  src="https://code.jquery.com/jquery-3.5.1.min.js"
  integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
  crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
  <a style="margin: 4px 4px 4px 4px !important" 
   data-toggle="modal" 
   data-target="#video_modal" 
   data-title="YouTube video title" 
   data-youtube-url="YouTube/url/">Montenegro protesters decry opposition's use of Serbian symbols
</a>

<a style="margin: 4px 4px 4px 4px !important" 
   data-toggle="modal" 
   data-target="#audio_modal" 
   data-title="Soundcloud item title" 
   data-soundcloud-url="soundcloud/url">Katarina Panic: Montenegro elections-The real winners are citizens
</a>
   
<div class="modal fade bd-example-modal-lg" id="video_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item youtube-iframe" src=""></iframe>
      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>
</div>

<div class="modal fade" id="audio_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
         <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item soundcloud-iframe" src=""></iframe>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  //SOUNDCLOUD MODAL SCRIPT
$('#audio_modal').on('show.bs.modal', function (event) {
  var source = $(event.relatedTarget); // Button that triggered the modal
  console.log(source + "Audio Modal JS");
  var title = source.data('title');
  var soundcloudurl = source.data('soundcloud-url'); // Extract info from data-* attributes

  // //MODIFY THE SOUNDCLOUD URL TO INCLUDE THE EMBED PART OF THE URL STRING
  var iframeLink = soundcloudurl.replace(soundcloudurl, 'https://w.soundcloud.com/player/?url=$1');

  // //SET THE TEXT AND THE SRC ATTRIBUTE OF THE MODAL
  var modal = $(this);
  modal.find('.modal-title').text(title);
  modal.find('.audio-iframe').attr("src",iframeLink);
});

//YOUTUBE MODAL SCRIPT
$('#video_modal').on('show.bs.modal', function (event) {
  var source= $(event.relatedTarget); // Button that triggered the modal
  console.log(source + "Video Modal JS");
  var title = source.data('title');
  var youtube_url = source.data('youtube-url'); // Extract info from data-* attributes

  //MODIFY THE YOUTUBE URL TO INCLUDE THE EMBED PART OF THE URL STRING
  var iframeLink = youtube_url.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, 'www.youtube.com/embed/$1');

  //SET THE TEXT AND THE SRC ATTRIBUTE OF THE MODAL
  var modal = $(this);
  modal.find('.modal-title').text(title);
  modal.find('.youtube-iframe').attr("src",iframeLink);

});
</script>
</body>
</html>

Thank you. Have a nice day.

Upvotes: 0

Related Questions