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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:03:01+00:00 2026-06-17T11:03:01+00:00

I thought that script within $(document).ready(…) would always execute after the DOM is loaded.

  • 0

I thought that script within $(document).ready(…) would always execute after the DOM is loaded. Hence, it wouldn’t matter if a $(document.ready(…) went in the head or in the body. However, the code below does not generate “apples” on the screen like I want it to. If I locate the giveApples() function at the bottom of the page though, it works.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(giveApples());
function giveApples() {
    $("#appleTree").html("apples");
}
</script> 
</head>
<body>
<div id="appleTree"></div>
</body>
<script>
//$(document).ready(giveApples());
</script>
</html>

Can anyone please correct my misconceptions about DOM, page loading, Script-tag location, (document).ready(), or anything else that is causing this problem? I’m still quite new to web programming.

  • 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-06-17T11:03:02+00:00Added an answer on June 17, 2026 at 11:03 am

    And it does not. The problem is that you’re not passing giveApples as argument, but its returned value, since you are calling it (because of the ()). To make it work, don’t put the parenthesis:

    $(document).ready(giveApples);
    

    By your current code, the value that is being passed to $(document).ready is undefined, since giveApples doesn’t return any value.

    You could also do:

    $(document).ready(function(){
        giveApples();    //However, if you access the 'this' keyword inside the giveApples function, it will point to 'window', and not 'document'
    });
    

    You can see what I explained above if you alert these two values:

    alert(giveApples);    //Shows the giveApples function body, properly
    alert(giveApples());  //Shows undefined, since giveApples is being called and does not return any value
    

    Its the same when you use DOM events (onload, onclick, etc). You do this way:

    window.onload = myFunction;
    

    And not:

    window.onload = myFunction();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought that by default my Regex would exhibit the greedy behavior that I
I thought that calling BeginInvoke more than once on the same delegate instance would
I thought that in principle Haskell's type system would forbid calls to impure functions
I need to be sure that a certain script within a page is executed
I want to reload my user profile from a script file. I thought that
I am building a new ruby script using GLI gem that uses Compass within.
I have six SQL queries that I script though R that each take a
I have a script that listens to a jabber server and responds accordingly. Though
I thought that by convention, only methods with an exclamation mark altered the object.
I thought that a 32-bit process could use at most 2GB RAM, since half

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.