SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast gilmeragency's Avatar
    Join Date
    Oct 2012
    Location
    Palm Springs, CA
    Posts
    34
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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...
    Code:
    $(document).ready(function (){
    /*SWITCH CASE HERE??? */
        $('#addcontent a').click(function(e){
            e.preventDefault();
            $('#inserthere').load(e.target.href + ' #thumbnails').hide().delay(100).fadeIn(500);
        });
    
    });
    Loader...
    Code:
    <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
    Code:
    <!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...
    Code:
    <!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>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by gilmeragency View Post
    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?
    Giving an html string to the .html() method results in the existing content being replaced. Using the .append() method results in the existing content remaining, with your new content being appended to it.

    Quote Originally Posted by gilmeragency View Post
    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)?
    No, that doesn't matter.

    Quote Originally Posted by gilmeragency View Post
    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?
    Inside of the click event for the links would seem to be the best place.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast gilmeragency's Avatar
    Join Date
    Oct 2012
    Location
    Palm Springs, CA
    Posts
    34
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Paul. I'll get cracking on this right away. If I have any questions, I will post again to this thread.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •