0

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 Answer 1

1

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
});
Sign up to request clarification or add additional context in comments.

6 Comments

Bull eyes dude, that fixed everything. One more question can we use that .live in $(document).ready();
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.
But this is not executing $(document).live("ready", function() { $(".accordion").accordion({ alwaysOpen: false, autoheight: false, clearStyle: true, active: true }); });
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?
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 afterwards Like to initiate accordion in classes with "accordion" i use $(document).ready(function() { $(".accordion").accordion({ alwaysOpen: false, autoheight: false, clearStyle: true, active: true }); });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.