Reputation: 2226
I currently have the following:
HTML:
<div class="content-class"></div>
JQuery Ajax:
$(document).ready(function() {
$(document).on("click", ".content-class", function(event) {
event.preventDefault();
var post = $(this);
$.ajax({
url: 'script.php',
type: 'POST',
data: { /* some data */ },
success: function(data) {
// load content from php file into .content-class
});
});
});
How do I change that function so that it loads the content from the PHP file into .content-class without having to click it (so that it does it on page load)?
Thanks.
Upvotes: 0
Views: 161
Reputation: 905
as PSL said, just move the ajax call outside of click event handler:
$(document).ready(function() {
$.ajax({
url: 'script.php',
type: 'POST',
data: { /* some data */ },
success: function(data) {
// load content from php file into .content-class
});
});
Upvotes: 1
Reputation: 108482
Just remove the click event listener and do the ajax straight away:
$(document).ready(function() {
$.ajax({
url: 'script.php',
type: 'POST',
data: { /* some data */ },
success: function(data) {
// load content from php file into .content-class
$('.content-class').html(data);
});
});
Upvotes: 3