cancel
Showing results for 
Search instead for 
Did you mean: 

Set cursor/focus on input field SimpleForm

Former Member
1,755

Hi All,

We have a requirement wherein on a page , there is a simple form with 4 input fields. On page load, requirement is to set cursor on first input field. We have an XML view, and in controller, in HandleRouteMatched, have written something like this,

this.getView().byId("<Input Field ID>").focus();

however its not working. Please suggest ways to achieve it.

Thanks and regards,Ruchi Agarwal

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

Is your target browser Safari?

Former Member
0 Kudos

No, I am using Chrome.

Thanks and regards, Ruchi Agarwal

Former Member
0 Kudos
  • Is your input field in a iFrame? If so, we need to make sure, if "this.byId()" method can access elements inside iFrame directly.
  • Can you try placing the delayedCall in onAfterRendering method in your controller? Debug to see if the input field is actually rendered when "this.byId("<Input Field ID>").focus()" is called.
  • If not, try increasing the timeout value (1st arg in the delayedCall). This is not an optimal solution but, this could help us know where the issue is. If it is just control rendering & function call sequence, increasing the timeout will uncover that.
Former Member
0 Kudos

Hi Murali,

. Control is not in iFrame. The xml view has a simple form, in that there are 4 pairs of sap.m.labels and inputs.

. Tried with putting Delayed call in onAfterRendering, but it didnt work. Tried with increased timeout value as well.

Thanks and regards, Ruchi Agarwal

Former Member
0 Kudos

Can you please share your view code? I will locally run it from my side. It will save us time.

Former Member
0 Kudos

I have the split app, on page load now cursor is getting set on the input field, with following code in onAfterRendering, however if I go to another page and come back to this view again, place same code in HandleRouteMatched, it does not then set focus again to the input field.

jQuery.sap.delayedCall(1000, this, function() { this.getView().byId("<input field>").getFocusDomRef().focus(); });

Thanks and regards, Ruchi Agarwal

View Entire Topic
Former Member

You could try this

jQuery.sap.delayedCall(0, this, function() {
    this.byId("<Input Field ID>").focus()
});
heterocigoto
Participant
0 Kudos

Works. I applied this in an input with event change to jump to another field:

view.xml:

 <Input id='in_werks' required="true" placeholder="2000" valueLiveUpdate="true" change='clearFields' />

Controller.js

jQuery.sap.delayedCall(0, this, function() {
this.byId("in_lgort").focus()
});

I am learning SAPUi5 so please consider any corrections to my code regarding guidelines.