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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:11:00+00:00 2026-05-25T15:11:00+00:00

I’d like to call functions I’ve defined within the document ready function of jQuery,

  • 0

I’d like to call functions I’ve defined within the document ready function of jQuery, but am having a bit of trouble. I have the following code:

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

    function test1() {
        alert('test1');
    }

    function test2() {
        alert('test2');
    }

    var test_call = '2';

    var fn = 'test' + test_call;

    // use fn to call test2

});

I don’t want to use eval, and window[fn] doesn’t seem to be working. The two test functions don’t appear to be indices in the window variable. I appreciate the help and knowledge.

  • 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-25T15:11:00+00:00Added an answer on May 25, 2026 at 3:11 pm

    All I can think of that doesn’t use eval() or some form of eval (passing a string to setTimeout() is a form of eval()), is to register the relevant function names on an object and then look up the function name on that object:

    jQuery(document).ready( function($) {
    
        function test1() {
            alert('test1');
        }
    
        function test2() {
            alert('test2');
        }
    
        // register functions on an object
        var funcList = {};
        funcList["test1"] = test1;
        funcList["test2"] = test2;
    
    
        var test_call = '2';
    
        var fn = 'test' + test_call;
    
        if (fn in funcList) {
            funcList[fn]();
        }
    
    });
    

    or the registration could be done in the definition of the functions. If they were global functions, they would be implicitly registered on the window object, but these are not global as they are scoped inside the document.ready handler function:

    jQuery(document).ready( function($) {
    
        var funcList = {};
    
        funcList.test1 = function test1() {
            alert('test1');
        }
    
        funcList.test2 = function test2() {
            alert('test2');
        }
    
        var test_call = '2';
        var fn = 'test' + test_call;
    
        if (fn in funcList) {
            funcList[fn]();
        }
    
    });
    

    Or, you could move the functions to the global scope so they are automatically registered with the window object like this:

    function test1() {
        alert('test1');
    }
    
    function test2() {
        alert('test2');
    }
    
    jQuery(document).ready( function($) {
    
        var test_call = '2';
        var fn = 'test' + test_call;
    
        if (fn in window) {
            window[fn]();
        }
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
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 have just tried to save a simple *.rtf file with some websites and

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.