Link to home
Start Free TrialLog in
Avatar of brucez
brucez

asked on

Problem with javascript eval() function

I've searched and I haven't been able to find this problem reported anywhere.  This is legacy code and has worked for years, but suddenly I have one user who is reporting a problem.  I've traced the problem to the eval() function.  When I run this script, the value displayed in the Result field is '103'.  The user with the problem  runs the script, she gets 'form1.InputField3.value'




<FORM name="form1" method="POST" action="javascript:">
  InputField1<input name="InputField1" type="text" value="101">
  <BR>
  InputField2<input name="InputField2" type="text" value="102">
  <BR>
  InputField2<input name="InputField3" type="text" value="103">
  <BR>
  InputField2<input name="InputField4" type="text" value="104">
  <BR>
  InputField2<input name="InputField5" type="text" value="105">
  <BR>
  Result<input size="50" name="Result" type="text" value="">
  <BR>
 
</FORM>
 
 
<SCRIPT LANGUAGE="JScript">
function evaltest(sInputNum)
{
	form1.Result.value = eval("form1.InputField"+sInputNum+".value");
}
 
evaltest('3')
</SCRIPT>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure if that parenthetical expression will work, but another alternative is:
function evaltest(sInputNum)
{
var key = "InputField"+sInputNum;
      document.form1.Result.value = document.form1[key].value;
}
Avatar of brucez
brucez

ASKER

I know how to rewrite it without the eval() function, but I'd rather not as this is old code that has been in place for years.  The actual application has 150 references to the eval() function.  I'd rather find some sort of configuration on the client side that to get it to work since this is just a small example I threw together.  

>>I know how to rewrite it without the eval() function, but I'd rather not
OK. Did you try my first post? It should work. Make sure you don't miss the semicolon immediately after value withing the quoted string.
Or try:

form1.Result.value = form1.InputField[sInputNum].value;

if the parentheses do not work.

Or (best, but more work) add an ID attribute to your elements and use:

form1.Result.value = document.getElementById(sInputNum).value;
Avatar of brucez

ASKER

Hielo's first suggestion works for my computer, but I'm still waiting for a response from the problem user.  Of the things suggested so far, that solution would be the least amount of work.  I'd still rather find out why it stopped working on one client machine and address that one client, though.
Avatar of brucez

ASKER

Badotz, I can't get

form1.Result.value = form1.InputField[sInputNum].value;

to work without renaming all the input tags and I'm afraid that I'll break something else if I rename them.  Your second solution is similar to what I came up with:
 
form1.Result.value = form1.getElementsByName('InputField')[sInputNum-1].value;

I don't like it, though  because I would still have to rename the input tags.
Avatar of brucez

ASKER

Hielo, I just heard back from the user and your first solution doesn't work for her.  The result field is blank.
What browser is the person using? Does she have javascript enabled? Are there any other errors on the page?

Also, try it with the document prefix:
 eval("document.form1.Result.value = document.form1.InputField"+sInputNum+".value;");
Avatar of brucez

ASKER

She's using IE 6.  There are no errors on the screen.
With IE 6, and code that's working for YOU and ME, then the issue is on her system. Most likely she has a cached page. Have her to:
Ctrl + F5
and try again. What I have given you definitely works on IE6. Other than that, you will need to look at the source code that the browser is getting. (Which means you may have to talk her through the steps to view source, and have her email it to you if she is in a remote location).