2

I can't seem to be able to focus a field in a form in extjs 4.

The doc lists a focus function for Text field (inherited from Component) but it doesn't do anything in terms of focusing to the input field.

Here's a sample code from the docs

Ext.create('Ext.form.Panel', {
    title: 'Contact Info',
    width: 300,
    bodyPadding: 10,
    renderTo: Ext.getBody(),        
    items: [{
        xtype: 'textfield',
        name: 'name',
        fieldLabel: 'Name',
        allowBlank: false 
    }, {
        xtype: 'textfield',
        id:'email',
        name: 'email',
        fieldLabel: 'Email Address',
        vtype: 'email'  
    }]
});

If I call Ext.getCmp('email').focus() nothing visible happens.

What's the correct way to focus a field in extjs 4?

Ben
  • 20,737
  • 12
  • 71
  • 115
  • Possible duplicate of [Set focus on Extjs textfield](https://stackoverflow.com/questions/6591371/set-focus-on-extjs-textfield) – Vadzim Feb 26 '18 at 13:49

4 Answers4

5

Sometimes a simple workaround is to slightly defer the focus call in case it's a timing issue with other code or even with the UI thread allowing the focus to take place. E.g.:

Ext.defer(function(){
    Ext.getCmp('email').focus();
}, 0);
Brian Moeskau
  • 20,103
  • 8
  • 71
  • 73
  • I would think that the defer option in the focus method would allow you to do this properly, but that does not seem to be the case. Thanks for the tip. +1 – Mike Nov 19 '13 at 06:57
2

Try this:

Ext.getCmp('email').focus(false, 20);
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Sabinus
  • 21
  • 1
2

There isn't anything wrong with your code. I've made a jsfiddle for it and it works fine. Are you wrapping code in and Ext.onReady()? Also what browser are you using?

rwilliams
  • 21,188
  • 6
  • 49
  • 55
  • on FF 6.0.2 mac. Definitely wrapped in onReady handler.. your jsfiddle works for me.. must be something somewhere else in my code that must be stealing focus.. I guess i can try trimming out the rest of the code to where it starts working again. At least i know focus _should_ work now, thanks – Ben Sep 24 '11 at 02:13
0

Rather than work around with a delay, look for what is getting focus instead. Look out for focusOnToFront property on the parent.

apfrod
  • 341
  • 3
  • 10