Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:05:58+00:00 2026-05-28T06:05:58+00:00

I have a document that loads some content into a div with JQuery’s .load()

  • 0

I have a document that loads some content into a div with JQuery’s .load() function. That loaded content then tries to execute Javascript from the original document. This fails, possibly for obvious reasons (I am a Javascript novice). Here is simplified code that illustrates my problem.

test.html:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <style type="text/css">
    div {
        background: #ccc;
        margin: 15px;
        height: 500px;
        width: 500px;
        border: 1px solid #666;
    }
    </style>
</head>
<body>
    <a href="#" class="loader">Load content into box below</a>
    <div id="loadbox"></div>
    <script type="text/javascript"> 

        jQuery(document).ready(function() { 

            $('a.loader').click(function() {
                $('#loadbox').load('loadme.html');
            });

            $('a.alerter').click(function() {
                alert("I've been clicked!");
            });

        });
    </script>
</body>
</html>

loadme.html:

<a href="#" class="alerter">Click me</a> for an alert.

The alert is triggered if I put the Javascript in the loaded content, but I have a large block of Javascript that I want to use with more than one div full of loaded content and I don’t want to keep loading the Javascript. Is there a way to access the Javascript in the original HTML from the loaded content? I don’t have a firm understanding of what order things are executed in, especially when I have loaded content into the page. Maybe this is my opportunity to learn! Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:05:59+00:00Added an answer on May 28, 2026 at 6:05 am

    DYou should use live() or delegate() because you are adding an element to the document (this is for jQuery < 1.7)

       jQuery(document).ready(function() { 
    
            $('a.loader').click(function() {
                $('#loadbox').load('loadme.html');
            });
    
            $('a.alerter').live("click", function() {
                alert("I've been clicked!");
            });
    
        });
    

    for jQuery > 1.7 you should use on() and delegate the handler to the body

       jQuery(document).ready(function() { 
    
            $('a.loader').click(function() {
                $('#loadbox').load('loadme.html');
            });
    
            $('body').on("click", "a.alerter", function() {
                alert("I've been clicked!");
            });
    
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some JQuery code that shows or hides a div. $(div#extraControls).show(); // OR
I have a function which loads some content when executed... IE... function load_product(product_id) {
So here's the scenario I have a list that loads some HTML into a
I have an XML document that I load in and try to search with
I have an HTML document that is using to embed a control. In some
I'm pulling some info from a database then putting it into a DIV and
I have long known that you can load style rules into a page dynamically
I have a page that contains a div. I load another page in to
I have these pages: information.php <div id='content'>Foo Bar</div> main.php <div id='main'></div> <script type=text/javascript> $(document).ready(function()
I'm delving into some AJAX and I'm trying to utilise jQuery. I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.