Joonas
Joonas

Reputation: 7303

Detect if text field is empty or not and trigger code once, unless the state changes

I want to detect if this text field is empty or not and then run some code based on that.

Question is: how can I run the code only once ...and then run it once again each and every time that the state of the text field changes during one focus


To further elaborate:

( The most important part in these jsfiddle's are shown in the console. )

Upvotes: 3

Views: 5038

Answers (2)

Oscar Jara
Oscar Jara

Reputation: 14187

You need to use a plugin called jQuery Text Change from ZURB. Does everything you want and more, also has a trigger action called 'hastext' and 'notext', take a look at:

http://www.zurb.com/playground/jquery-text-change-custom-event

there are also demos there.

I made this for you (using that plugin).

LIVE DEMO:

http://jsfiddle.net/oscarj24/UtwNz/

Hope this helps :-)

Upvotes: 1

RichardTowers
RichardTowers

Reputation: 4762

You could keep a hold of the value of the field, and only trigger if the value's state has changed: http://jsfiddle.net/xk6A5/9/

Upvotes: 0

Related Questions