Reputation: 1748
If I make an AJAX request and want to call all functions that were set up by $(document).ready()
. How can I do it? Thank you
Upvotes: 3
Views: 4848
Reputation: 2005
$(document).ready();
If that doesn't work, try this:
function startup() {
// All your functions in here.
}
$(document).ready(startup);
And after your AJAX request is finished:
startup();
Upvotes: 6
Reputation: 129792
The easiest way is to use a shared function:
$(document).ready(function() {
setup();
});
$.post('/', {}, function() {
setup();
}, 'json');
But if you're using it to re-assign listeners, you would probably be able to get away with assigning them before they're created, like this:
$(document).ready(function() {
$(document).delegate('.my-button', 'click', function() { });
});
Using this code, all .my-button
clicks will be handled by your custom function, regardless of whether the button existed in your DOM upon DOMReady.
Note that:
$(document).ready(function() { ... });
can be minimized to $(function() { ... });
.on
over .delegate
: $(document).on('click', .my-button', function() { });
$('#close-parent').delegate
over $(document).delegate
Upvotes: 3
Reputation: 9912
Instead of triggering document.ready by hand (which would be bad practice IMHO), make a function that's called setup
which sets up all listeners etc. and invoke this function when you need to re-apply things etc.
Upvotes: 0