Matt Walker
Matt Walker

Reputation: 25

loading content into a div with jquery

Hey All - I am having a bit of trouble with a jquery function. I have a nav bar and I want when a user clicks on the nav bar to have the page load into a div (#content)

The code I have works for the first click, but when I try to click on another item it shows the loading image, but does not put the new content in the div (it leaves the old content). Any thoughts?

$(document).ready(function() {

   $('.cat').live("click",function() {
        var section = $(this).attr("id");
        $.ajax({
            type: "POST",
            url: "../soccer/nav/" + section + ".php",

            beforeSend:  function() {
                $('div#content').hide();
                $('div#loading').show();
            },

            success: function(html){
                $('div#loading').hide();
                $("div#content").replacewith(html);
            }
        });     
        return false;
    });
});

Any help would be greatly appreciated!

Upvotes: 1

Views: 996

Answers (4)

Kamyar
Kamyar

Reputation: 18797

Try to use jQuery.Load event to make use of ajax and html populating at the same time.

Upvotes: 0

Kelly
Kelly

Reputation: 41501

The problem is the .replacewith() method. You are actually replacing the #content div with the new content. Just use .html() instead.

Upvotes: 1

jotapdiez
jotapdiez

Reputation: 1466

Try with:

$.post("../soccer/nav/" + section + ".php", null, function(data)
{
   $('#loading').hide();
   $("#content").html(data);
});

Upvotes: 0

Newtang
Newtang

Reputation: 6544

You don't need $("div#content"). You can just do $("#content")

$("#content") means find the node with the id "content".

Upvotes: 0

Related Questions