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.

EDIT:
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)
{
//True
}
else
{
//false
}*/
}

Open in new window

richardsimnettAsked:
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.

leakim971MultitechnicianCommented:
>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 ?
leakim971MultitechnicianCommented:
Try to use call() or apply() : http://odetocode.com/Blogs/scott/archive/2007/07/05/function-apply-and-function-call-in-javascript.aspx
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) {
			alert("true");
		}
		else {
			alert(b);
		}
	}

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
leakim971MultitechnicianCommented:
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.

leakim971MultitechnicianCommented:
Using apply, I have something working fine :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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 )] );
			if(b)
				alert("true");
			else
				alert("false");
		}
	}
	
	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;
	}

</script>
</head>
<body>
<input value="abcde" id="id1" />
<input value="fghij" id="id2" />
</body>
</html>

Open in new window

richardsimnettAuthor Commented:
Thanks, works great now.
leakim971MultitechnicianCommented:
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
JavaScript

From novice to tech pro — start learning today.