JQuery (AJAX)-Add Content to DIV/Remove Content from DIV

Hi Everyone,

Let’s say I already have content in a div. When I use JQuery to add other content to that div, does it automatically remove the existing content? If not, what is the best way to modify the code so I don’t have content loaded on top of preexisting content? And one way or the other, does it matter whether or not that content was put there by JQuery (for example, default content that was already there when page loads)? Last, for more efficient code, I want to use a switch-case to listen for which link was clicked so the appropriate content will be loaded; where would be best to put the switch-case?

JQuery code…


$(document).ready(function (){
[B]/*SWITCH CASE HERE??? */[/B]
    $('#addcontent a').click(function(e){
        e.preventDefault();
        $('#inserthere').load(e.target.href + ' #thumbnails').hide().delay(100).fadeIn(500);
    });

});

Loader…


<div id="addcontent">
        <a href="thumbnails.html">Slide Show</a>
        <a href="about.html">About Galilee</a>
        <div id="inserthere"></div>
    </div>

Loaded Content 1


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Slides</title>
</head>
<body>
<h1>Slideshow Images</h1>
<p>These are some of our latest menu items</p>
<div id="thumbnails">
    <img src="images/arrozcontortillas_tn.jpg" alt="Arroz con tortillas" />
    <img src="images/burrongigante_tn.jpg" alt="Burron Gigante" />
    <img src="images/brideandgroom_tn.jpg" alt="Bride and Groom" />
    <img src="images/dulcedeboda_tn.jpg" alt="Dulce de Boda" />
    <img src="images/chipsconguacamole_tn.jpg" alt="Chips con Guacamole" />
    <img src="images/tortillasdearina_tn.jpg" alt="Tortillas de Harina" />
    <img src="images/taquitosandcheese_tn.jpg" alt="Taquitos con Queso" />
    <img src="images/sopadegallina_tn.jpg" alt="Sopa de Gallina" />
    <img src="images/sopadecamarones_tn.jpg" alt="Sopa de Camarones" />
</div>
</body>
</html>

Loaded Content 2…


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>About Us</title>
</head>
<body>
<h1>About Galilee Leadership</h1>
<p>About the Pastor</p>
<div id="about">
 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
</div>
</body>
</html>

Giving an html string to the .html() method results in the existing content being replaced. Using the [URL=“http://api.jquery.com/append/”].append() method results in the existing content remaining, with your new content being appended to it.

No, that doesn’t matter.

Inside of the click event for the links would seem to be the best place.

Thanks, Paul. I’ll get cracking on this right away. If I have any questions, I will post again to this thread.