1

I am actually creating an input field dynamically in lightning , and i want to set focus on the field, as soon after it is created. So i wrote this :

    loadEditableCmp : function(component, event, helper) {         
        var container = component.find("xid-fld-container");
        var ftype = component.get("v.fieldType");

        if(ftype == "currency"){
            var param = {
              "aura:id" : "xid-fld",                
              "value": component.getReference("v.fieldValue"),
              "class": "slds-input xcs-max-w",
              "blur" : component.getReference("c.handleBlur")
           };

          $A.createComponent("ui:inputNumber",param,function(cmp) {
             container.set("v.body", cmp);
             console.log("field loaded");
             cmp.focus();
          });
      }
   },
   handleBlur : function(component, event, helper) {
      console.log("## element blured");
  }

There is no error generated on the focus method when used as above, but there is no focus on the field after its load. Does anyone ever achieve this?

3

As i thought, it only need a small delay before setting the focus. So in the callback function should only set the focus in a setTimeout function, for the small delay between setting the body and the focus :

       $A.createComponent("ui:inputNumber",param,function(cmp) {
            container.set("v.body", cmp);
            console.log("field loaded");
            //add only a small delay so that it has the time 
            //to set the body and trigger the focus
            setTimeout(function(){ cmp.focus(); }, 200);

        });
| improve this answer | |
  • Interesting - thanks for the update. I was having problems setting focus to an input field on a modal after I'd created it, so maybe it's a timing issue. I'll have to test this out. Thanks! – Florissant53 Jun 21 '17 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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