I would like all AJAX loaded content in my app to be evaluated by my application JQuery script, the same as normal loaded content is. e.g. JQuery scans through the AJAX loaded content for selectors, like 'modal box links' etc.
All my JavaScript is in the normal document.ready, which works fine for normal HTTP loaded pages:
$(document).ready(function(){
// my apps javascript
});
I would like to use something like .ajaxComplete to fire a re-run of everything contained in document.ready to evaluate newly loaded AJAX content for jquery selectors.
$(document).ajaxComplete(function(){
// Re-run all my apps javascript
})
Is there some code I can put in .ajaxComplete to do this?
Hope this makes sense, please let me know if it doesn't, and I will edit question details.
I would like all AJAX loaded content in my app to be evaluated by my application JQuery script, the same as normal loaded content is. e.g. JQuery scans through the AJAX loaded content for selectors, like 'modal box links' etc.
All my JavaScript is in the normal document.ready, which works fine for normal HTTP loaded pages:
$(document).ready(function(){
// my apps javascript
});
I would like to use something like .ajaxComplete to fire a re-run of everything contained in document.ready to evaluate newly loaded AJAX content for jquery selectors.
$(document).ajaxComplete(function(){
// Re-run all my apps javascript
})
Is there some code I can put in .ajaxComplete to do this?
Hope this makes sense, please let me know if it doesn't, and I will edit question details.
Share Improve this question edited Jan 20, 2011 at 2:22 jamesmortensen 34k11 gold badges102 silver badges123 bronze badges asked Jan 20, 2011 at 2:07 David BarlowDavid Barlow 4,9741 gold badge30 silver badges24 bronze badges 2- Can you explain more about what you're looking for? I feel like my answer is missing something still, and I want to make sure you find the answers you seek. – jamesmortensen Commented Jan 20, 2011 at 2:13
- @jmort253 - Thanks for your help, hunter's answer is pretty much what I was looking to understand. – David Barlow Commented Jan 20, 2011 at 2:44
2 Answers
Reset to default 12you could encapsulate everything in your document.ready
into a function and just call that function again to re-bind
or...
a better approach would be to take advantage of the live()
and delegate()
jQuery methods so that all current and future elements matching those selectors will be bound to these events as well.
example using live()
: http://api.jquery./live/
$('.clickme').live('click', function() {
//all elements that exist now and elements added later
//with this class have this click event attached
});
example using delegate()
: http://api.jquery./delegate/
$("table").delegate("td", "hover", function(){
//all current and future td elements in all current tables
//have this hover event attached
});
What you need to do is define your code in a function instead of anonymously. Anonymous functions are not reusable, but if you write a function, then that function can be bound to multiple events.