views:

37

answers:

1

I have a function to load a html part into a element in main index page kinda like this

$(".ajax").click(function() {
         //load a page into a element
});

This works But the HTML part which i have just loaded also have links that need to trigger same function above, but it does not.

Until I save that function in a separate .js file and load in the main index file as well as all other files from where I need to trigger that function even though these internal files are going to loaded into the the first main file .

Is there any way for a function in the index file to run from the html document that is loaded inside a element.

+1  A: 

Your question is not completely clear, but if you want elements with the ajax class inside the loaded content to trigger the event, you can use live(), like this:

$(".ajax").live("click", function() {
         //load a page into a element
});
Max Shawabkeh
Bull eyes dude, that fixed everything. One more question can we use that .live in$(document).ready();
Starx
Yes, you can use it at any point, and from that point on anything that matches the selector no matter when it is loaded will trigger the event.
Max Shawabkeh
But this is not executing$(document).live("ready", function() { $(".accordion").accordion({ alwaysOpen: false, autoheight: false, clearStyle: true, active: true });});
Starx
A `document.ready` can only occur once per page - loading content into an element doesn't trigger it. Why would you want to live-bind a once-occurring event?
Max Shawabkeh
I have used accordions on many places, and all of them are loaded through .load() function so, i need to write same line of code in every html file that is loaded. Like before do you know how to initiate a plugin for all matching selectors that are present or will be loaded afterwardsLike to initiate accordion in classes with "accordion" i use$(document).ready(function() {$(".accordion").accordion({ alwaysOpen: false, autoheight: false, clearStyle: true, active: true });});
Starx
In that case you should probably stuff that into a function, then call it in your AJAX handler or pass it as a callback to the `load()` method. You could also use the global AJAX functions: http://docs.jquery.com/Ajax_Events#Global_Events
Max Shawabkeh