ericbae
ericbae

Reputation: 9644

Highlighting text in a div, which is loaded via AJAX

I have a div element, which is loaded via AJAX. Once it's loaded, I want to highlight the text in it (the usual process, click your mouse and drag to highlight).

But since DOM is already loaded, the mouse related events are not registered for these new div's, right?

If I have a div like this

<div id="parent"></div>

And after jQuery "get" call, it becomes

<div id="parent">
  <div id="child">Hello this content is loaded via AJAX</div>
</div>

How do I rebind the div "#child" to mouse-related events? And exactly which events do I rebind them to?

Upvotes: 0

Views: 527

Answers (2)

Bharat Patil
Bharat Patil

Reputation: 1408

For binding events to dynamically loaded contents use jquery .live refer officially documentation as follows http://api.jquery.com/live/

Upvotes: 0

Radu
Radu

Reputation: 8699

You're looking for .live().

$("#child").live({
    click: function() {
        // do stuff
    },
    mouseover: function() {
        // do stuff
    },
    mouseout: function() {
        // do stuff
    }
});

Bind as many events as you like!

Upvotes: 2

Related Questions