Skip to Content
0

Set cursor/focus on input field SimpleForm

Feb 08, 2017 at 06:33 AM

81

avatar image

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

10 |10000 characters needed characters left characters exceeded

Is your target browser Safari?

0

No, I am using Chrome.

Thanks and regards, Ruchi Agarwal

0
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Murali Lakshmanan Feb 08, 2017 at 06:58 AM
0

You could try this

jQuery.sap.delayedCall(0, this, function() {
    this.byId("<Input Field ID>").focus()
});
Share
10 |10000 characters needed characters left characters exceeded
Ruchi Agarwal Feb 08, 2017 at 08:17 AM
0

Hi Murali,

I just tried this, but it didn't work. Please suggest inputs.

Thanks and regards,Ruchi Agarwal

Show 4 Share
10 |10000 characters needed characters left characters exceeded
  • 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.
0

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

0

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

0

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

0