1

I am working in JavaScript coding. I have created a text area with name OQ_0 and value "0". When i use eval() method for that field in JavaScript it is giving the value undefined. The below are the part of JavaScript code

var tempOpenQtyStr = "document.InitiateReturnsForm.OQ" + "_" + 0; 
var tempOpenxQtyStr = eval(tempOpenQtyStr).value;
alert('Manuals =' + document.InitiateReturnsForm.OQ_0.value);
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);

Output:

Manuals = 0
eval(tempOpenxQtyStr ) = 0 --- Here it is suppose to show "[object]"
eval(tempOpenxQtyStr).value = undefined.

Kindly help me out what is change to do. Thanks in advance.

2
  • 1
    Could you please post the html part relating to this, just so that we can see the DOM, Thanks Commented Mar 10, 2010 at 12:50
  • Stop using eval()!! This whole thing is a bad idea. Commented Mar 10, 2010 at 13:43

3 Answers 3

5

Why not just use document.InitiateReturnsForm["OQ_" + 0].value?

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

Comments

1

Try

alert('eval(tempOpenxQtyStr ) = ' + eval(tempOpenQtyStr));
alert('eval(tempOpenxQtyStr).value = ' + eval(tempOpenQtyStr).value);

In the second and third alert you are evaluating the second variable which stores the value of the first evaluated object. That's why the error occurs.

Comments

1
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));

Since you put a string, not an object, inside tempOpenxQtyStr, it evaluates that string and returns 0.

alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);

Here you're using a method on a variable that contains a string. That doesn't work. It doesn't have that method, that's why it returns undefinied.

You might want to try doing eval(tempOpenxQtyStr.value) instead of eval(tempOpenxQtyStr).value since the last one does basically nothing, just evaluating an object and then fetching the objects value (it doesn't eval the value itself).

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.