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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:06:28+00:00 2026-05-23T21:06:28+00:00

Is it correct to create functions inside of $(document).ready(function() { like so: $(document).ready(function() {

  • 0

Is it correct to create functions inside of

$(document).ready(function() {

like so:

$(document).ready(function() {
     function callMe() {

     }
 });

The function inside of the .ready() does not have to call before DOM is ready and event inside of the ready() is triggered.

Just to clarify a little bit – here’s the code which would illustrate the problem:

$(function() {
    var ind = 0;

    // some event is executed and changes the value of the ind

    // another event which affects the ind variable

    // and another one - after this event we call our function


    // there's another event - and we call our function again

The function which I need to call needs the updated value of the ind variable – which I guess I could pass as a parameter, but is there a better way of doing it?

Also – another important thing is that the function() in question can also change the value of the ind variable – for instance incrementing it (ind++).

  • 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-23T21:06:29+00:00Added an answer on May 23, 2026 at 9:06 pm

    Yes, you can do that, it’s just a matter of scope.

    If you only need to access callMe() from within $(document).ready(function() { }), then it’s fine to put the function there, and offers some architecture benefits because you can’t access the function outside of that context.

    If you need to use the callMe() function outside of document ready though, you need to define the callMe() function outside of that context.

    function callMe() {
      // Do Something
    }
    
    $(document).ready(function() {
      callMe();
    });
    

    UPDATE

    Based on your clarification, you have two options:

    1) DECLARE variable outside of ready(), but then define variable inside of ready():

    var someVariable;
    function callMe() {
      someVariable++;
      alert(someVariable);
    }
    
    $(document).ready(function() {
      someVariable = 3;
      callMe(); // Should display '4'
    });
    

    2) Within ready(), define variables using window.yourVariable = 'whatever';

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

Sidebar

Related Questions

How do i run a PHP function inside jQuery click event. I have the
What is the correct syntax to create objects in javascript that will work across
I got this logic in a control to create a correct url for an
I want to programatically create an NSTextView. How can I determine the correct frame
Can anyone tell how correct the following code below. Iam tryin to create a
I have created a simple Asp.Net custom control which automatically combines all the correct
Correct me if I'm wrong, but a build is a compile, and not every
In many situations I find that I need to create long-living values inside a
I've always passed arguments to a function like so: setValue('foo','#bar') function setValue(val,ele){ $(ele).val(val); };
I have a function which takes two current class level member variables and sets

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.