user2094538
user2094538

Reputation: 31

jQuery: .load() a page within an already loaded page

I load a menu with a .load() and it works well. But I can't seem to load a sub-menu from this pre-loaded menu the same way.

Here is my menu:

<!doctype html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <title>Titre de la page</title>
  <link rel="stylesheet" href="style.css">
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="loader.js"></script>
</head>
<body>
<div id="wrapper">
    <div id="menu">
        <img src="images/logo.jpg" /><br>
        Illustrator &amp; Art Director
        <p><br><br><br>
        <a href="#" id="about">About Ātman</a><br>
        <a href="#" id="illustration">Illustration</a><br>
        <a href="#" id="graphic">Graphic Art</a><br>
        <a href="#" id="contact">Contact</a></p>
    </div>
    <div id="main"></div>
    <div id="boutons"></div>
</div>
</body>

And here is the loader.js:

$(document).ready(function(){
    // load index page when the page loads
    $("#main").load("illustration.html #aacc");
    $("#boutons").load("boutons.html #illustrations");
    $("#about").click(function(){
        $("#main").load("about.html");
    });
    $("#illustration").click(function(){
        $("#main").load("illustration.html #aacc");
        $("#boutons").load("boutons.html #illustrations");
    });
    $("#graphic").click(function(){
        $("#main").load("graphic.html #navet");
        $("#boutons").load("boutons.html #graphic");
    });     
    $("#contact").click(function(){
        $("#main").load("contact.html");
    });
$("#img1").click(function(){
    $("#main").load("illustration.html #aacc");
});
$("#img2").click(function(){
    $("#main").load("illustration.html #navet");
});
    $("#lol").click(function(){
        alert('you clicked me!');
    });
});

And here is the sub-menu (boutons.html) loaded from the menu:

<body>
    <div id="illustrations">
        <table border="0" width="200" cellpadding="1" cellspacing="1">
            <tr>
                <td><a href="#" id="img1"><img src="images/bouton/BI1.jpg" alt="" /></a></td>
                <td><a href="#" id="img2"><img src="images/bouton/BI5.jpg" alt="" /></a></td>
                <td><a href="#" id="lol">About Ātman</a></td>
            </tr>
        </table>
    </div>
</body>

And here is the content I want to load from the illustration.html:

<body>
    <div id="aacc">
        <img src="images/I/AACC.jpg" alt="AACC" />
        <div id="en">NO ADVERTISING EXCEPT BETC   poster for open door day (concept, art direction, paper sculpture)</div>
        <div id="fr">PAS DE PUB SAUF BETC   affiche pour la journée porte ouverte (concept, direction artistique, sculpture en papier)</div>
    </div>
    <div id="navet">
        <img src="images/I/navet.jpg" alt="navet" />
        <div id="en">NAVET  illustration for comics cover (paper sculpture)</div>
        <div id="fr">NAVET  illustration pour couverture de bande dessinée (sculpture en papier)</div>
    </div>  
</body>

I made the "lol" for the test and it doesn't work. Nothing happens.

Thanks in advance for your help :)

Upvotes: 2

Views: 2092

Answers (2)

LeGEC
LeGEC

Reputation: 51890

You should bind events after the nodes have been loaded in the DOM, e.g. :

instead of

$(document).ready(function(){
     $("#boutons").load("boutons.html #illustrations");
     //...

     //#illustrations is not loaded yet, $('#img1') and $('#img2') are empty
     $("#img1").click(function(){
         $("#main").load("illustration.html #aacc");
     });
     $("#img2").click(function(){
         $("#main").load("illustration.html #navet");
     });
})

you can :

$(document).ready(function(){
     $("#boutons").load("boutons.html #illustrations",
         //this callback is called *after* .load() has completed
         function(){
             $("#img1").click(function(){
                 $("#main").load("illustration.html #aacc");
             });
             $("#img2").click(function(){
                 $("#main").load("illustration.html #navet");
             });
         }
     );

Another way (cleaner IMHO) is to delegate events to a node that is here from the beginning, and never leaves the DOM :

$(document).ready(function(){
    // this basically says "if any child of '#boutons' matches the '#img1'
    // selector, execute this click handler"
    $('#boutons').on( 'click', '#img1', function(){
        //inside this handler, 'this' refers to the '#img1' item
        $("#main").load("illustration.html #aacc");
    });
    $('#boutons').on( 'click', '#img2', function(){
        $("#main").load("illustration.html #navet");
    });

    //...
    $("#boutons").load("boutons.html #illustrations");
});

For extra details about the on function, go check the jQuery doc.

Upvotes: 2

andri
andri

Reputation: 1021

I believe event delegation is what you are looking for.

Depends on your version of JQuery, you may want to use live or on instead of click.

Upvotes: 0

Related Questions