evavienna
evavienna

Reputation: 1109

Trigger Click on a DIV after Click on a Button which opens a Modal

I have a Loop with a lot of post and each post has a video-modal which opens after a click on a <button></button> element. Below the button is the html part for the modal. To autoplay the video (after opening the modal) I need to trigger a click on the div element with the classclass="start". Here is the setup

<button class="btn btn-default" id="video-modals-loop" data-toggle="modal" data-target="#001">Play Video</button>

<div class="modal loop-video-modals" id="001">
    <div class="modal-dialog">
        <div class="modal-header">
            <button class="close" aria-hidden="true" type="button">Close</button>
        </div>                                  
        <div id="#product-video">   
            <div class="start" data-src="https://t-e-s-t.com/"></div>                   
        </div>
    </div>
</div>

I already tried things like

$(document).on('click', '#video-modals-loop', function() {
    $(this).closest("#video-modals-loop").find(".lazyframe").trigger('click');  
}); 

but Failed miserably. Any idea how to trigger a click after open the modal on the mentioned div?

Edit: It is a bootstrap modal and the start div is separate for each modal.

Upvotes: 1

Views: 2765

Answers (2)

evavienna
evavienna

Reputation: 1109

After reading the jQuery Documentation about .closest() and the following answer on Stackoverflow i was able to solve my problem. The following code works fine. Hope this helps others too.

$(document).on('click', '#video-modals-loop', function() {
    $(this).closest('div').find('.start').click();

    // IF needed you can use a timeout to delay the click event
    // setTimeout(function(){ 
        // $(this).closest('div').find('.start').click();
    // },1);

    return false;
});

Upvotes: 1

Ganesh Putta
Ganesh Putta

Reputation: 2671

If u are trying this on desktop its okay, But it wont work in mobile devices coz auto play doesn't support in mobile devices, Take care of that

https://www.tutorialrepublic.com/codelab.php?topic=faq&file=autoplay-youtube-video-within-bootstrap-modal

One more thing, below code doesn't work here properly, because our snippet output window also works as mobile/small device, just copy code run it from your system then it works fine.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of Autoplaying YouTube Video inside Bootstrap Modal</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
    .bs-example{
    	margin: 20px;
    }
    .modal-content iframe{
        margin: 0 auto;
        display: block;
    }
</style>
<script type="text/javascript">
$(document).ready(function(){
    /* Get iframe src attribute value i.e. YouTube video url
    and store it in a variable */
    var url = $("#cartoonVideo").attr('src');
    
    /* Remove iframe src attribute on page load to
    prevent autoplay in background */
    $("#cartoonVideo").attr('src', '');
	
	/* Assign the initially stored url back to the iframe src
    attribute when modal is displayed */
    $("#myModal").on('shown.bs.modal', function(){
        $("#cartoonVideo").attr('src', url);
    });
    
    /* Assign empty url value to the iframe src attribute when
    modal hide, which stop the video playing */
    $("#myModal").on('hide.bs.modal', function(){
        $("#cartoonVideo").attr('src', '');
    });
});
</script>
</head>
<body>
<div class="bs-example">
    <!-- Button HTML (to Trigger Modal) -->
    <a href="#myModal" class="btn btn-lg btn-primary" data-toggle="modal">Launch Demo Modal</a>
    
    <!-- Modal HTML -->
    <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">YouTube Video</h4>
                </div>
                <div class="modal-body">
                    <iframe id="cartoonVideo" width="560" height="315" src="//www.youtube.com/embed/YE7VzlLtp-4?autoplay=1" frameborder="0" allowfullscreen></iframe>
                </div>
            </div>
        </div>
    </div>
</div>     
</body>
</html>                                		                            

Upvotes: 0

Related Questions