"
It seems that the magnificPopup() function is a setup function. You call it when your document loads, and it sets up its own event handlers to handle the onclick.
So why did it take 2? clicking the link the first time fires the setup events, but doesnt activate the modal. The next time you click, you’re actually invoking BOTH your own handler (again) and the handler that the setup event set up on the item.
The below does not work on its own $('.image-link').magnificPopup({type:'image'});
Something I ran into a few weeks back using the Mustache template engine. I need to wrap all my click events with the document part $(document).on('click', '.popup', function(e){} for it to work.
is exactly the same as the following condensed jQuery syntax:
$(function () {
...
});
You can learn more about this from the jQuery documentation about the jQuery callback where it says: This function behaves just like $( document ).ready()
I understand the document ready regarding the short hand with jquery. The problem is trying to use this plugin and fire it on single click within the Mustache template engine.
As outlined in an older thread using a diffrent plugin, I had the same issue which @m3g4p0p described as event delegation.
Example
This works when not using MustacheJS $('.popup').magnificPopup({});
But when using MustacheJS - I need to add $(document) $(document).on('click', '.popup', function(e){}
I’ve updated/created a new jsfiddle so you can see the Mustache template working with the popup needing 2 clicks.
Thanks Paul, I did try this before and new this would be tested
It looks like things work, though on the bigger sacle and after much testing this does not work within my setup. I’m sure of it.
When I use this snippet within the CMS running Musatche and other frameworks, the video just opens in a new window. The popup dose’t fire.
I’ve already done lots of testing with this and know that every click handler used has needed $(document) to function within the Mustache template engine.
Just wondering though, is it not possible to set this up using $(document) somehow as mention in my last post?
Did you know that your jsfiddle example already has a secret document.ready javascript code wrapper?
If you click on the JavaScript + jQuery 2.1.3 dropdown, you’ll see that the Load Type is On DOM Ready which puts all of your JavaScript code inside of $(document).ready(...)
Here’s an update of the JSFiddle that removes the secret onready wrap from the code, so that you can use it more clearly within the code with:
Did you know that your jsfiddle example already has a secret document.ready javascript code wrapper?
Thanks Paul, I am aware of this. This is just for testing. In my dev setup I have lots of functions already running within the dom ready. This is not the problem.
The problem which I’ve just realised is that I’m using AJAX and loading JSON.
If you have a look at the new codepen (from an old thread) and fire up a video, you’ll see it works in the first istance. Now close the video and change the result using the dropdown and try opening a video again - this is the problem.
Now if we change your suggestion and revert back to my orignal snippet, it works everytime. But needs 2 clicks.