How do you call a function from javascript and recieve returned value?

I have a JavaScript function call in a String. I replace all occurrences of "this" with the appropriate reference to the current object. It returns a boolean. I want JavaScript to call the function within the string after string manipulation and give me the boolean value it returns.

I've tried doing this through eval but it doesn't work. If I set the string to "false" or "true" it functions correctly but when I add actual code ex: "test(this, selectContainsValue)" it produces an error.

I just tried to use a one argument function and it works properly. However, my method must take in any number of arguments.

The reason the function is in a string is for the simple fact it is located in a JSON file. All functions in the string are dynamic and given from the server...

basic example:
for (var i = 0; i < forms.fields.length; i++) {
  var s = "f(this.value)".replace(/this/g, "document.getElementById(" + forms.fields[i].inputid + ")");

/*if (execute f and recieve return value)

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>but when I add actual code ex: "test(this, selectContainsValue)" it produces an error.

What is "this" for you in this case ? Generaly the context when using this is in an object/class on when you call the function from an HTML element. When you run "eval" what do you expect "this" is ?
Try to use call() or apply() :
var s = "f"; // s is the function to run
	for(var i=0;i<forms.fields.length;i++) {
		var b = eval( s + ".call(" + forms.fields[i].inputid + ")" );
		if(b) {
		else {

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Could you post your json string ?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Using apply, I have something working fine :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	var forms = {"fields":[{"inputid":"id1"},{"inputid":"id2"}]}
	window.onload = function() {
		for(var i=0;i<forms.fields.length;i++) {
  		        var s = "f"; // the function f, can be dynamic 
			var myFunction = eval(s);
			var b = myFunction.apply(null, [document.getElementById( forms.fields[i].inputid )] );
	function f(o) { // fake function. return true only if the value of the passed object is equal to "abcde"
		if(o.value == "abcde") return true;
		return false;

<input value="abcde" id="id1" />
<input value="fghij" id="id2" />

Open in new window

richardsimnettAuthor Commented:
Thanks, works great now.
You're very welcome! Thanks for the points!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.