jQuery selectors with ajax

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

jQuery selectors with ajax

Post by GeXus »

I have some data that's being loaded in via jQuery's .load function... I want to add a hover attribute to items within that loaded div, but it doesn't appear to work because the actual elements aren't in the source, since their being loaded via ajax.

For example, here's what I have..

Code: Select all

 
$(document).ready(function(){
$('#divTest li').hover(function() {
  alert('test');
})
});
 

Code: Select all

 
<div id="Data"></div>
 
This is the div that's being loaded from another page with the following content...
 
<div id="divTest">
<ul>
<li>some data</li>
</ul>
</div>
 
Any ideas how to get this to work? Thanks!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: jQuery selectors with ajax

Post by pickle »

What I've done in cases like that is have a specific function that re-parses the DOM. I then call that function after load() is called. The result is that after load is done, the DOM is updated, then events are attached to the newly created elements.

Also, if you wrap your code not just in [syntax=php][/syntax] tags, but in [syntax=javascript][/syntax] and [syntax=html][/syntax] tags, it'll be syntax highlighted.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply