-
AuthorPosts
-
August 14, 2015 at 10:31 pm #488040
Hi,
First I want to thank you for that awesome theme.
I’m having a lot of fun customizing it, and the support you provide through the forum has already helped me tremendously.So far I managed to find answers to my problems without the need to open my own thread but I can’t find a solution for the following issue:
Note : English is not my native language so I uploaded 2 pages to help you understand what I want to achieve :
On the Contact page, a click on the image starts playing the video, so far so good, that’s what I want.
The issue on the Home page can be seen when clicking the first portfolio grid ‘s item from the right (“Bande Annonce de la Fondation Gan”).
(all the portfolio entries are set to “display the preview text only” so I can display a video in the AJAX preview)As you can see, on the Home page’s AJAX preview, it seems my jQuery script is not working at all (it doesn’t hide the video and the other functions don’t work).
I’m not really good with JavaScript so I guess that’s not the best way to achieve what I want, but it does work as intended on the Contact page.Here is the code I have in my Preview text Settings for the said item (the same code I have on the Contact page):
[av_one_full first]<img id="video-overlay" class="aligncenter" src="http://siliconvalway.com/wordpress/wp-content/uploads/2015/08/GAN.png" alt="" /> <iframe id="my-video" class="aligncenter" src="https://www.youtube.com/embed/kx6DLToz6XQ?autoplay=0&;rel=0&;showinfo=0" width="750" height="422" frameborder="0" allowfullscreen="allowfullscreen"></iframe>[/av_one_full]
And here is the code I have in my functions.php child theme’s file :
wp_enqueue_script( 'jquery' ); function add_custom_script(){ ?> <script type="text/javascript"> // play video when click on image jQuery(document).ready(function($) { function autoPlayOn(el){ el.attr('src',el.attr('src').replace('autoplay=0','autoplay=1')); }; $('#my-video').hide(); $('#video-overlay').click(function() { autoPlayOn($('#my-video')); $('#video-overlay').hide(); $('#my-video').show(); }); }); </script> <?php } add_action('wp_footer', 'add_custom_script');
I was guessing my jQuery selectors could be the issue but I don’t know much about it.
I’d really appreciate your help on this!Regards
Cyril
August 16, 2015 at 11:12 pm #488456Bump and little update :
After hours spent on google and stackoverflow, I really think I need to change the way I select the html elements that I want with jQuery if I want my script to work on the AJAX preview.
Maybe by using a .find() or .filter() method on the AJAX results, but I don’t really now how to work on the AJAX query.
Can you please look into it and tell me at least where I should keep digging?Regards
Cyril
August 17, 2015 at 4:13 pm #488972Hi!
If your trying to target elements that have been loaded via AJAX then you need to use .live or .delegate if I remember right, https://api.jquery.com/category/events/event-handler-attachment/.
I recommend hanging out in #jquery on irc.freenode.net. I learned quite a bit by just reading about other peoples problems in there.
Best regards,
Elliott- This reply was modified 9 years, 4 months ago by Elliott.
August 21, 2015 at 9:02 pm #491602Hey Elliott,
Thank you for your help!
It seems the .live method is deprecated as of jQuery 1.7 so I used .on() and I managed to solve the issue.
My jQuery code looks like that :
jQuery(document).ready(function($) { $("body").on("click", "#video-overlay", function(playVideo){ function autoPlayOn(el){ el.attr('src',el.attr('src').replace('autoplay=0','autoplay=1')); }; $('#my-video').show(); autoPlayOn($('#my-video')); $('#video-overlay').hide(); }); });
I definitely need to learn more about JavaScript. I guess me not really understanding the DOM was the main cause of the problem.
Thanks again and keep up the good work. -
AuthorPosts
- The topic ‘jQuery script not working in portfolio's AJAX preview’ is closed to new replies.