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

  • Home
  • SEARCH
  • 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 9317387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:51:56+00:00 2026-06-19T02:51:56+00:00

In the code below I assign the gobal variable myDiv a jQuery element. But

  • 0

In the code below I assign the gobal variable “myDiv” a jQuery element. But then inside the “test()” function it seems the myDiv variable is declared but not defined. I have to reassign it $(‘myDiv’) again before I can do anything with it.

The Bogus variable xyz seems to be reachable just fine though…

<!DOCTYPE html>
<htm>
<head>
<title>Javascript Test Code</title>
<script src="js/jquery-1.9.0.min.js"></script>
<script>

var App = App || (function () {

  var myDiv = $('#myDiv'), // define a global jQuery element variable.  
        xyz = 'xyz';       // and a bogus variable as a test.  

  function test()
  {
    console.log('running test...','myDiv is', myDiv); // length is 0
    console.log('xyz is ', xyz); // says xyz

    myDiv = $('#myDiv'); // after selecting with jQuery...

    console.log('running test...','myDiv is', myDiv); //length is 1
    console.log('xyz is ', xyz); // still says xyz
  }

  return {test:test} 

}());

$(function(){

  App.test();

});

</script>
</head>
<body>
<div id="myDiv"></div>
</body>
</html>
  • 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-19T02:51:57+00:00Added an answer on June 19, 2026 at 2:51 am

    There is no access to elements from the head of the document because nothing is rendered. This line

    var App = App || (function () {
    

    will execute the self executing function because App at that point is undefined.

    Once the self executing function is ran, it will look for $('#myDiv') but since the element does not exist, it will not be found (this is always why you get a length of 0 for the elements array which matches the selector).

    Later, when you attempt to access the App it will have already been built.

    $(function(){
     App.test();
    });
    

    Which means that the call to App returns the already built App from the self executing function, with the values already stored from when it executed in the head of the document.

    When you call the function test the state that was loaded is reflected. However, lower in the function you issue

    myDiv = $('#myDiv'); // after selecting with jQuery...
    

    once the page has already been loaded (since we are inside of the jquery ready callback already). This will find the element on the page since the DOM has already been rendered and update the value.

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

Sidebar

Related Questions

The Jquery code below works ok with firefox, Safari, Opera but not with IE.
I've the code below that works great in my ready function in jquery, when
In the code below: (function (){ function test(){};//function var test;//undefined var printTest = typeof
The code below sets a select menu with UI selectmenu by $('.anyclass').selectmenu(); but the
The code below will validate required fields within currently visible fieldsets, but only if
I use the below code to assign a given by the user character to
The code below will extract icon from file and convert it to png but
I wonder what's wrong with my code below: // Assign hour set in the
I want to assign a value to a checkbox. The code below doesn't seem
For some reason, the code below doesn't work. I need it to assign 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.