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?
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
});
});
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 Use the "selector" parameter for event delegation.
$('#PARENT').hoverIntent({
over: function() {
console.log($(this));
},
out: function(){
console.log($(this));
},
selector: '.CHILD'
});
Source: hoverIntent Documentation
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
});
});
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 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.
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.
});
© 2022 - 2024 — McMap. All rights reserved.
"hoverintent"
should be an event not a method :X – Gainsborough