Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery – Better understanding of .live(); Re: jQuery – Better understanding of .live();

#116429
TheDoc
Member

If you find that `.on()` isn’t working it’s probably because you’re trying to use it on an element that wasn’t there on initial page load. This means you’ll need to apply the `on()` to something that’s already on the page and then listen for things that bubble up the DOM as @tbwiii mentions.

If we use a Twitter panel as an example… Your `#twitter` div is going to be on the page when it loads, but likely you’re pulling in all of your tweets via an API. To be able to capture a hover event in jQuery on a tweet and you try this:

$(‘.tweet’).on(‘hover’, function() { // something });

It’s not going to work because `.tweet` has been loaded in via AJAX. You’ll need to do this:

$(‘#twitter’).on(‘hover’, ‘.tweet’, function () {
// something
});

Basically exactly what tbwiii said, just wanted to give another example!