Bagwell
Bagwell

Reputation: 2226

Load content in div on page load

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

Answers (2)

what is sleep
what is sleep

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

David Hellsing
David Hellsing

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

Related Questions