Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

2 Answers

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}, 
       Sow[{
          InputField[Dynamic[tx[[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
add comment

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};
Grid[{
  {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
add comment

Your Answer

 
discard

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.