1

I have a simple responsive site built with Twitter bootstrap.

I have a video sitting in a modal that is called and auto plays on the click of a button.

I want to call my function on page load instead of on a button click.

<a href="#" class="btn btn-default" data-toggle="modal" data-target="#videoModal" 
data-theVideo="https://www.youtube.com/embed/44H1gmn9tBA">VIDEO</a>

<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="videoModal" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-body">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <div>
                <iframe width="100%" height="350" src=""></iframe>
            </div>
        </div>
    </div>
</div>

and the .js

<script type="text/javascript">
  autoPlayYouTubeModal();
function autoPlayYouTubeModal() {
  var trigger = $("body").find('[data-toggle="modal"]');
  trigger.click(function () {
      var theModal = $(this).data("target"),
          videoSRC = $(this).attr("data-theVideo"),
          videoSRCauto = videoSRC + "?autoplay=1";
      $(theModal + ' iframe').attr('src', videoSRCauto);
      $(theModal + ' button.close').click(function () {
          $(theModal + ' iframe').attr('src', videoSRC);
      });
      $('.modal').click(function () {
          $(theModal + ' iframe').attr('src', videoSRC);
      });
  });
  }
</script>

Everything above works as intended but I have been trying to do this (Below) in order to call the autoplay function on page load.

<script>
$("document").ready(function(){

$("#videoModal").trigger("click");
});
</script>

When I paste the video modal.trigger function into the console it returns the modal but the modal and video never launch.

Appreciate any help.

1
  • Should $("document") not be $(document)? Commented Apr 19, 2015 at 12:46

1 Answer 1

2

It should be

$(function(){
       $('[data-toggle="modal"]').trigger("click");
});

Because the modal window click event handler is having the autoplay option.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.