views:

5582

answers:

4

How can I apply some code to the content of an ajax loaded tab? I tried using $(document).ready inside the loaded content, but that prevented css styles from loading (don't know why).

Is there a callback function? Should I use $(document).ready and styles inside the loaded document in some other way?

If using $(document).ready inside the loaded document is fine, should I also include references to jquery and its plugins in it?

+4  A: 

What code are you using to load the content through ajax? If you using a jQuery command like load or ajax then I would recommend putting your code inside the callback function. For example, using load

$('#myUITab').load('anotherPage.html', function() {

    // put the code you need to run when the load completes in here

});
Russ Cam
I am using a tab's method:$('#content').tabs( 'add' , 'tabs/editor.php' , data[i].nombre);
Gerardo
+4  A: 

Have you tried the load event? This should be called, when the contents of the tab have been loaded.

In general you shouldn't treat the loaded element as a new page and call the $(document).ready. This is not a new page, but some new elements added to the DOM. All ajax methods feature a callback method that is invoked, when the data are successfully loaded.

kgiannakakis
The load event is applied to every tab, should I use that event and retrieve the selected tab inside it to apply the new code?Every tab has different DOM in it, such as tables or forms.
Gerardo
I haven't used the event myself. See if you can get the tab loaded from either the event or ui parameter that is passed to the callback method. Unfortunately the documentation doesn't explain these parameters. Using the DOM of the tabs will work, but it's a "dirty" solution.
kgiannakakis
Following conventions, I'd imagine that event.target.id will be the id of the tab that has had content loaded into it. Use firebug and console.log to investigate :)
Russ Cam
A: 

Another way to do this is by using ajaxComplete:

$("#loading_comtainer").ajaxComplete(function( event,request, settings ){
   alert("ajaxCompleted");
});
vsync