Lucas César
Lucas César

Reputation: 111

How to get an element by index in ExtJS

I have a form with some elements in it. What I want to do is check if there is a specific element in position x. Is there a way so I can get the element x by its index value?

Upvotes: 1

Views: 2717

Answers (2)

Ruan Mendes
Ruan Mendes

Reputation: 92334

If you want to access the third textfield, you can use Ext.ComponentQuery

var thirdTextField = formcomponent.query('textfield')[2];

or using down

var thirdTextField = formcomponent.down('textfield:nth-child(3)'):

See https://fiddle.sencha.com/#fiddle/i2k

    var form = Ext.create('Ext.form.Panel', {
        title: 'Simple Form',
        bodyPadding: 5,
        width: 350,
        layout: 'anchor',
        defaults: {anchor: '100%'},

        defaultType: 'textfield',
        items: [{
            fieldLabel: 'First Name',
            name: 'first',
            allowBlank: false
        }, {
            fieldLabel: 'Middle Name',
            name: 'middle',
            allowBlank: false
        }, {
            fieldLabel: 'Last Name',
            name: 'last',
            allowBlank: false
        }],
        renderTo: Ext.getBody()
    });

    form.down('textfield:nth-child(3)').setValue('Johnson')
}    

Upvotes: 2

Luiguis
Luiguis

Reputation: 402

//0-based index
var element = form.items.getAt(index);

Upvotes: 0

Related Questions