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 7073963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:59:35+00:00 2026-05-28T05:59:35+00:00

I’m just starting out with jQuery and want to use $(document).ready() to dynamically generate

  • 0

I’m just starting out with jQuery and want to use $(document).ready() to dynamically generate some HTML every time the page loads. It works fine when I use inline JavaScript code like this (this example is obviously simplified but it shows the behavior of the problem):

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <script type="text/javascript">
        $(document).ready(function(){
            $("#container").html("<p>Hello world</p>");
        });
    </script>
    <title>Test</title>
</head>
<body>
    <div id="container"></div>
</body>
</html>

However, if I link to an external JavaScript file and replace the inline JavaScript with this:

$(document).ready(start());

where the code for start is

function start(){

$("#container").html("<p>Hello world</p>");

}

nothing happens. I put an alert box in the external start() function and it pops up fine, so the code inside the function is being executed. The console also doesn’t show any errors. However, if I replace

$("#container").html("<p>Hello world</p>");

with

document.getElementById("container").innerHTML = "<p>Hello world</p>";

the console gives an error, saying document.getElementById(“container”) is null.

I’d really appreciate it if someone could point me in the right direction on this.

  • 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-28T05:59:36+00:00Added an answer on May 28, 2026 at 5:59 am

    This part of your code:

    $(document).ready(start());
    

    should be:

    $(document).ready(start);
    

    So, in the first example, you’re calling start() immediately before the document is ready and passing it’s return value to $(document).ready() which is NOT what you want.

    In the second example, you’re passing a reference to the start() function to $(document).ready() and it will call the start() function later when the document is ready which IS what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.