Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom events without trigger
    primarykey
    data
    text
    <p>I have the following javascript function that I use to create a basic event handler for images being loaded in any given container...</p> <pre><code>function onImagesLoaded($container, callback) { var $images = $container.find("img"); var imgCount = $images.length; if (!imgCount) { callback(); } else { $images.each(function () { $(this).one("load error", function () { imgCount--; if (imgCount == 0) { callback(); } }); if (this.complete) $(this).load(); }); } } </code></pre> <p>I currently call that with something like this...</p> <pre><code>onImagesLoaded($("div.image-container"), function() { console.log("Images loaded"); }); </code></pre> <p>This is fine and it works, but I want to be able to attach it to elements like this...</p> <pre><code>$("div.image-container").on("imagesloaded", function() { console.log("Images loaded"); }); </code></pre> <p>This isn't a <em>"my code doesn't work - please fix it"</em> type of question, because it does work and does exactly what I want, functionality-wise. I'm asking this question as I can't find any help relating to how to do it the way I want to do it and I'm trying to learn something new. Everything I've read about jQuery custom events requires you to manually trigger them, and that's not what I'm trying to do here.</p> <p>I'm saying that I want to attach an event handler to an element and then assign my function to that element <em>because</em> of that event handler. Maybe I need to parse the elements in the DOM manually to do this - I don't know.</p> <p>Can anyone at least point me in the direction of how this can be done (if it can)?</p> <p><strong>Edit</strong></p> <p>I started working on overriding the existing <code>on()</code> function in a similar way to how KevinB suggested below. The code he supplied covered a couple of issues I had with my version so I merged the two together and came up with a working version....</p> <pre><code>var $_on = $.fn.on $.fn.on = function () { switch (arguments[0]) { case "imagesloaded": onImagesLoaded(this, arguments[1]); break; default: return $_on.apply(this, arguments); break; } }; </code></pre> <p>Using that I can now attach custom event handlers using <code>.on()</code>, just as I said I wanted in the question above.</p> <p>In hindsight, the question title was worded badly. It should have been about just that - overriding <code>on()</code>.</p> <p><strong>Another Edit</strong></p> <p>After further thought on the matter I decided to create a <code>createEvent()</code> method so I could add custom events to jQuery's <code>on()</code> without hassle. If anyone comes across this page and is looking for that, you can find it here...</p> <p><a href="http://johncmolyneux.blogspot.co.uk/2013/12/how-to-override-jquery-on-and-add.html" rel="nofollow">http://johncmolyneux.blogspot.co.uk/2013/12/how-to-override-jquery-on-and-add.html</a></p> <p>There's also a link on that page to the jquery.extend library on GitHub, which I will add to whenever I make something that I think should be added.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload