TextField - How to check the Focus


Is there a way to find out which text field has the current focus, something like:

if (textField.hasFocus){
code in here…..

I've tried a couple of different combinations of this but can't get it working.


4 Answers

  • You need to attach "focus" and "blur" events to text field components so you can track if component has focus ("focus" event) or has not ("blur" event). You can use simple flag to see do text field has focus:

    function CreateTextField(options) {
        var tf = Ti.UI.createTextField(options);
        //define flag
        tf.hasFocus = false;
        tf.addEventListener('focus', function() {
            this.hasFocus = true;
        tf.addEventListener('blur', function() {
            this.hasFocus = false;
        return tf;
  • You need to just add an eventListener for the textField. Something like this.

    textField.addEventListener('focus', function() {
        do something amazingly awesome!
  • you could try something using the 'focus' (and 'blur' event) :

    function doSomething(e){
        Ti.API.info(e.source+' has focus');
  • The best way is to add an "id" (myId for example) in each Textfield and get back its value from the Event (change, blur, …), such as 'e.myId' or 'e.source.myId'. You need to have a usable 'id' (something you can use).