Skip to Content
avatar image
Former Member

Set cursor/focus on input field SimpleForm

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • avatar image
    Former Member
    Feb 08, 2017 at 06:58 AM

    You could try this

    jQuery.sap.delayedCall(0, this, function() {
        this.byId("<Input Field ID>").focus()
    });
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 08, 2017 at 08:17 AM

    Hi Murali,

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

    Thanks and regards,Ruchi Agarwal

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      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

  • 4 days ago

    A bit late to answer but if someone just landed here by googling: You should be able to set the focus every time the page shows with the snippet below. More: https://stackoverflow.com/a/48559689.

    onInit: function() {
      this.setInitialFocus(this.byId("input"));
    },
      
    setInitialFocus: function(control) {
      this.getView().addEventDelegate({
        onAfterShow: function() {
          setTimeout(function() {
            control.focus();
          }.bind(this), 0);
        },
      }, this);
    },
    
    Add comment
    10|10000 characters needed characters exceeded