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.

This question already has an answer here:

I'm having trouble detecting when my InputForm supplied String variable has a value. After looking at:

How to compute values only if input fields are filled?

I tried a StringQ test:

Clear[foo]
foo[t_] := If [ StringQ[t], {t, "defined"}, {t, "undefined"} ]

DynamicModule[{symbol},
 Column[{
   Row[{"Text field:", InputField[Dynamic[symbol], String]}],
   foo[ Dynamic[symbol] ]
   } ]
 ]

Before the field is supplied this gives me what I expected:

before field keyed

but after keying the field, I see that my test hasn't worked:

after keying text

Based on

How can I make sure all InputFields have values before processing a nb form?

I tried:

Clear[foo]
foo[t_] := If [ !Equal[t, ""], {t, "defined"}, {t, "undefined"} ]

DynamicModule[{symbol = ""},
 Column[{
   Row[{"Text field:", InputField[Dynamic[symbol], String]}],
   foo[ Dynamic[symbol] ]
   } ]
 ]

but this produces a kind of odd unevaluated If block:

other try

I'm clearly doing something wrong with how I'm testing or using the input variable or form, but it's not obvious to me what that is?

share|improve this question

marked as duplicate by Kuba, Yves Klett, Sjoerd C. de Vries, Artes, rm -rf Dec 7 '13 at 7:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
it's a simple mistake: Dynamic@foo[symbol], Dynamic gives head. Try StringQ[Dynamic["test"]] –  Kuba Dec 6 '13 at 15:56
    
I agree that this is a duplicate, but the issue seems to be the wrong use of Dynamic, the code will do what is expected when using Dynamic[foo[symbol]] instead of foo[Dynamic[symbol]] and that isn't explained in the linked duplicate question IMO. The best answer I could find seems to be John Fultz's to this question, which I think would be a better duplicate link, but probably someone knows a good candidate on ME... –  Albert Retey Dec 7 '13 at 11:21

1 Answer 1

As canvassed often here it is better practice to stick your functions in initialization. Additionally it is best to think about Dynamic as updating pixels on the screen. So find the expression that gets rendered on the screen and then wrap it in Dynamic. The code below works fine:

DynamicModule[{symbol = Null},
 Column[{
   Row[{"Text field:", InputField[Dynamic[symbol], String]}],
   Dynamic@foo[symbol]
   }],
 Initialization :> {foo[t_] := 
    If[StringQ[t], {t, "defined"}, {t, "undefined"}]}
 ]
share|improve this answer

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