javascript - JQuery Button Not Clickable Second Time? -
i have used youtube iframe api play youtube video on button click. html code gets replaced youtube iframe video code on button click. when video ended previous html code gets replaced. how button clicks not working second time. please !
this code html , jquery
jquery(document).ready(function () { jquery('.play_button').click(function(e){ var html_sidebar = jquery(this).closest(".sidebar-container2").html(); var player; e.preventdefault(); player = new yt.player('video-play', { height: '345px', width: '100%', videoid: '7o0nkfdp2s4', events: { 'onready': onplayerready, 'onstatechange': onplayerstatechange } }); // autoplay video function onplayerready(event) { event.target.playvideo(); } // when video ends function onplayerstatechange(event) { if(event.data === 0) { jquery('#tertiary1').html(html_sidebar); } } }); jquery('#watch-video').click(function (e) { var html_sidebar = jquery(this).closest(".sidebar-container2").html(); var player; e.preventdefault(); player = new yt.player('video-play', { height: '345px', width: '100%', videoid: '7o0nkfdp2s4', events: { 'onready': onplayerready, 'onstatechange': onplayerstatechange } }); // autoplay video function onplayerready(event) { event.target.playvideo(); } // when video ends function onplayerstatechange(event) { if (event.data === 0) { jquery('#tertiary1').html(html_sidebar); } } }); }); <div role="complementary" class="sidebar-container2" id="tertiary1"> <div id="video-play" class="widget-1 widget-first widget-last widget-odd widget_video"> <div class="textwidget"> <div class="video_container"> <span class="play_button"></span> <div class="video-text"> <h3>delivering change foundation</h3> <p>dcf independent organization partners governments , communities accomplish nation transformation.</p> <p><a id="watch-video" class="yellow_arrow">watch video </a></p> </div> </div> </div> </div> </div> <script type='text/javascript' src="https://www.youtube.com/iframe_api"></script> what can ? new jquery tried find solution no results.
you need use event delegation original html markup being altered.
the event handler bound initial #watch-video gets replaced , added again, events lost.
jquery(document).on('click','#watch-video',function(){ }); you can use static parent instead of jquery(document), delegate event in case is:
jquery('#tertiary1').on('click','#watch-video',function(){ }); update:
if have multiple button triggers, use combined selector,
jquery('#tertiary1').on('click','#watch-video,.play_button',function(){ })
Comments
Post a Comment