I am using a For loop for printing five InputFields. The loop gets its values from the variable tx. I want to compare the values dynamically with 10 and print the result. But, the comparison is going out of loop, why?

tx = {4, 1, 6, 7, 8};
Reap[For[i = 1, i <= 5, i++,
    Sow[ {InputField[tx[[i]]],Dynamic[If[(tx[[i]] < 10), "right", "wrong"]]}]]]

2 Answers 2


This can be fixed by using With on the index. Also you missed a Dynamic in the inputfield:

 tx = {4, 1, 6, 7, 8};

 yourLoop=Reap[For[i = 1, i <= 5, i++,
    With[{i = i}, 
          Dynamic[If[(tx[[i]] < 10), "right", "wrong"]]

 yourLoop[[2, 1]] // Grid //Panel

Output Illustration

There's some more details about the With[{i=i}] solution in my answer to the question Generating dynamic Inputfields


I do not know now how to automate the InputField using an index. But why not simply write them down? There are only 5 of them?

Here is an example using 2 InputFields

tx = {1, 2};
  {InputField[Dynamic[tx[[1]]], Number, ContinuousAction -> True], 
   Dynamic[If[tx[[1]] < 10, "right", "wrong", "huh?"]]},

  {InputField[Dynamic[tx[[2]]], Number, ContinuousAction -> True], 
   Dynamic[If[tx[[2]] < 10, "right", "wrong", "huh?"]]}

  }, Frame -> True]

enter image description here

If I find a way to use a Table or such, will update, but it did not work when I tried it.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.