Thamali Wijewardhana
Thamali Wijewardhana

Reputation: 512

Extjs Component Ext.ComponentQuery.query access component by a variable name

I am using Extjs Ext.ComponentQuery.query as below to access a component named combo_box_2.

var i = 2;
var name2 = "combo_box_" + i;
Ext.ComponentQuery.query('combo[name=combo_box_2]')[0].bindStore(error_store);

The above code works fine. But I need to access the component by giving variable name name2 as below instead of giving the name straight away. But it does not work. Can anyone help me with this.

Ext.ComponentQuery.query('combo[name=name2]')[0].bindStore(error_store);

Upvotes: 0

Views: 320

Answers (1)

And-y
And-y

Reputation: 1524

You have to concatenate the string with the variable part in order to have the name be part of the query.

var i = 2;
var name2 = "combo_box_" + i;
var query = 'combo[name=' + name2 + ']';
Ext.ComponentQuery.query(query)[0].bindStore(error_store);

You could also use the new ES6 feature template strings to archive it.

var i = 2;
var name2 = "combo_box_" + i;
Ext.ComponentQuery.query(`combo[name=${name2}]`)[0].bindStore(error_store);

Upvotes: 3

Related Questions