0

I have several objects on a page... I know the name of one of the objects and it's stored in a variable called editorId. How can I access that object's methods? editorId.someFunction() doesn't work because the value of the variable is the object's name.

3 Answers 3

6

If it's a global variable you could just do var editor = window[editorID];.

If it's local you could do var editor = (new Function('return ' + editorID))();.

Then you could do editor.blah();

Sign up to request clarification or add additional context in comments.

1 Comment

This is the best solution, using window[editorID].someFunction(); should work just fine.
0

If you are referring to an HTML element on the page, you can use:

document.getElementById(editorId);

If you are referring to the name of a JavaScript object, you can try eval(), which is not very efficient:

var obj = eval(editorId);
// obj.someFunction()

Comments

0

use document.getElementsByName(editorId). Alternatively if your name is actually an ID, use getElementById()

http://www.w3schools.com/jsref/met_doc_getelementsbyname.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.