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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:32:49+00:00 2026-06-10T23:32:49+00:00

Is it possible to call a method from an object using a string? var

  • 0

Is it possible to call a method from an object using a string?

var elem = $('#test');             //<div id="test"></div>
var str = "attr('id')";  

//This is what I'm trying to achieve
  elem.attr('id');                 //test

//What I've tried so far  
  elem.str;                        //undefined
  elem.str();                      //Object [object Object] has no method 'str'
  var fn = eval(str);              //attr is not defined
  eval(elem.toString()+'.'+str);   //Unexpected identifier

//Only solution I've found so far, 
//but is not an option for me 
//because this code is in a function 
//so the element and method call
//get passed in and I wouldn't know
//what they are
  eval($('#test').attr('id'));     //test
  • 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-10T23:32:50+00:00Added an answer on June 10, 2026 at 11:32 pm

    UPDATE

    This is my final, working answer:
    After running this code in the console

    theMethod = 'attr("id","foo")'.match(/^([^(]+)\(([^)]*)\)/);
    jQuery('#post-form')[theMethod[1]].apply(jQuery('#post-form'),JSON.parse('['+theMethod[2]+']'));
    

    The post-form element now has a new ID, no problems at all. This works for methods that take multiple arguments, a single argument or no arguments at all. Recap:

    theMethod = theInString.match(/^\.?([^(]+)\(([^)]*)\)/);
    //added \.? to trim leading dot
    //made match in between brackets non-greedy
    //dropped the $ flag at the end, to avoid issues with trailing white-space after )
    elem[theMethod[1]].apply(elem,JSON.parse('['+theMethod+']'));
    

    That’s the safest, most reliable approach I can think of, really


    What ever you do DON’T USE EVAL:

    var theMethod = 'attr(\'id\')';
    //break it down:
    theMethod = theMethod.match(/^([^(]+)\(.*?([^)'"]+).*\)$/);
    //returns ["attr('id')", "attr", "id"]
    elem[theMethod[1]](theMethod[2]);//calls the method
    

    It’s the same basic principle as you’d use with any objects (remember that functions are objects all on their own in JS – and jQuery objects are, well, objects, too). This means that methods can be accessed in the exact same way as properties can:

    $('#foo').attr('id') === $('#foo')['attr']('id');
    

    So just break the string apart, and use the method name like you would an object property and you’re all set to go.

    Just remember: When all you have is the eval hammer, everything looks like your thumb.
    Brendan Eich


    If there is a chance of multiple arguments being passed to whatever method, you can sort of work your way around that, too (I think – well: logic dictates, but it’s rather late and logic is getting beat up by Gin pretty bad now):

    theMethod = theMethod.match(/^([^(]+)\(([^)]+)\)$/);
    //["attr('id','foo')", "attr", "'id','foo'"] --> regex must now match quotes, too
    elem.theMethod[1].apply(elem,JSON.parse('['+theMethod[2]+']'));
    

    This applies the method of whatever element/object you’re dealing with to itself, thus not changing the caller context (this will still point to the object within the method) and it passes an array of arguments that will be passed to the called method.

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

Sidebar

Related Questions

Possible Duplicate: Why is the 'this' keyword required to call an extension method from
Possible Duplicate: javascript object, access variable property name? I'm trying to call a method
Is it possible to call a COM method from PowerShell using named parameters? The
How, if it's possible, can you call a method from another method's return ?
any idea how (if even possible) to call java method with optional parameters from
I am trying to test a WCF service using the basichttpbinding endpoint from the
I am trying to call my BHO method from the javascript. The problem is
i trying to call a COM-Object which is described here Is it possible to
Is it possible to call a class method of all objects in a list?
In Scala, is it possible to call a member method without having to call

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.