Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"]]}]]]
share|improve this question
up vote 2 down vote accepted

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

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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