jQuery.on() - how initialize as soon as elements have been loaded
Asked Answered
H

4

5

I'm trying to add the hoverIntent plugin to elements on my page, however some elements will be added to the DOM later on, which means I have to bind the plugin to future elements as well. How do you do that? I have seen the same questions on SO a few times, but nobody has a clear solution?

Hypnotist answered 5/7, 2012 at 10:26 Comment(5)
are you trying to wait untill; images or jquery functions loaded?Castera
Use a hoverintent plugin that is implemented as an event so it supports delegation.. "hoverintent" should be an event not a method :XGainsborough
@barlas Actually no, the new content is loaded using InfiniteScroll.Countrywoman
@Gainsborough I have no clue what you're telling me, as far as I know, there' only one HoverIntent plugin and only one way you can implement it.Countrywoman
@AnriëtteCombrink I mean you can implement it as an event - events support delegation so it would be really simple. Like this.Gainsborough
C
2

There's no support in jQuery to do this; it's something the plugin itself must support. The most common approach is to simply re-initialize the plugin after adding new content; which normally isn't a problem.

Another option is to use the liveQuery plugin, and do something like this;

$('yourSelector').livequery(function () {
    $(this).hoverIntent({
        // blah
    });
});
Casework answered 5/7, 2012 at 10:30 Comment(2)
I wrote a re_init() function containing all the code that needed to be re-initialized once new content was loaded. I only realized there's more code then the hoverIntent that needed to be re-initialized once I started with this function.Countrywoman
For anyone else who stumbles upon this problem: hoverIntent supports event delegation without the need for extra plugins. See my answer below for an example.Nudd
N
9

Use the "selector" parameter for event delegation.

$('#PARENT').hoverIntent({
    over: function() {
        console.log($(this));
    },
    out: function(){
        console.log($(this));
    },
    selector: '.CHILD'
});

Source: hoverIntent Documentation

Nudd answered 20/1, 2014 at 23:19 Comment(0)
C
2

There's no support in jQuery to do this; it's something the plugin itself must support. The most common approach is to simply re-initialize the plugin after adding new content; which normally isn't a problem.

Another option is to use the liveQuery plugin, and do something like this;

$('yourSelector').livequery(function () {
    $(this).hoverIntent({
        // blah
    });
});
Casework answered 5/7, 2012 at 10:30 Comment(2)
I wrote a re_init() function containing all the code that needed to be re-initialized once new content was loaded. I only realized there's more code then the hoverIntent that needed to be re-initialized once I started with this function.Countrywoman
For anyone else who stumbles upon this problem: hoverIntent supports event delegation without the need for extra plugins. See my answer below for an example.Nudd
O
0

The best way to achieve this is to raise a custom event when you add the new elements to the page. Subscribers to this event can then call the appropriate hoverIntent initialisers.

There will be more native support in the form of mutation observers, however browser support is currently next to nil.

Listening for the DOMSubtreeModifed event is not reliable and causes major performance issues.

Oaks answered 5/7, 2012 at 10:30 Comment(1)
Mutation observer support is looking good now caniuse.com/mutationobserver/embedLammergeier
C
0

try to change your jquery structure;

ABC = {
   init: function() {//all function names goes here
       ABC.myFunction1();
       ABC.myFunction2();
       ABC.myFunction3();
   },
   myFunction1: function() {
     //code
   },
   myFunction2: function() {
     //code
   },
   myFunction3: function() {
     //code
   }//dont put comma last one
}

$(function() {
    ABC.init();//calls all functions after everything is initialized and ready.
});
Castera answered 5/7, 2012 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.