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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:46:13+00:00 2026-06-15T04:46:13+00:00

Possible Duplicate: How to execute a JavaScript function when I have its name as

  • 0

Possible Duplicate:
How to execute a JavaScript function when I have its name as a string

Struggling with this one, and I can’t seem to find a good resource on it.

Background: I’m creating a step system, and I’m passing the direcition/order within an attribute data-step="1". This controls the ID that will be shown (that parts easy), but also the function that needs to be called in order to grab the correct information.

The question is basically, how can I call a function who’s name I need to build dynamically?

IE:
step1(); step2(); Except I want to dynamically ADD that number in there.

// In an essense, what I'm trying to achieve:
// It's always called step and then followed by a number

[step + directionNumber](); // which isn't working

Also trying to avoid using eval since we all know it’s evil 🙂

  • 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-15T04:46:14+00:00Added an answer on June 15, 2026 at 4:46 am

    Use

    window["step" + directionNumber](); 
    

    When you write in the global scope

    function someName(){
    }
    

    you’re defining a property of the global object (window) with name someName and value the function.

    It’s equivalent to

    window.someName = function(){
    }
    

    So you can call your function as window.someName(); or, more useful here, as window['someName']();

    A better solution would probably be to define an array of functions :

    var stepFunctions = [];
    stepFunctions[0] = function(){};
    

    then you can call them using stepFunctions[directionNumber]();

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

Sidebar

Related Questions

Possible Duplicate: How to execute a JavaScript function when I have its name as
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: How do you execute a dynamically loaded JavaScript block? I have seen
Possible Duplicate: What do parentheses surrounding a JavaScript object/function/class declaration mean? What does this
Possible Duplicate: How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
Possible Duplicate: Javascript how do you find the caller function? I'm experimenting with javascript/jQuery
Possible Duplicate: javascript string exec strange behavior I have a regex and i am
Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) Can
Possible Duplicate: Javascript infamous Loop problem? I have the following: function test(a,b,c){ console.log(a+b+c); }
Possible Duplicate: JavaScript scope and closure What is this for? (function(){ //The code to

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.