Reputation: 17020
ExtJS 4.1.
Is there something like Ext.button.click();
method on Ext.button class?
Is it possible to programmically "click" a button with one method?
Upvotes: 17
Views: 45538
Reputation: 20234
Since I needed it for many buttons, it was easier to implement an override on the button class, which adds a click function:
Ext.define('Ext.override.Button',{
override:'Ext.button.Button',
click:function() {
this.getEl().dom.click();
}
})
After this override has been added to the code base, the following works like a charm:
Ext.getCmp("MyButton").click()
Unlike fireEvent or fireHandler, this will work with all kinds of buttons - no matter whether they have a click event or a handler, or whether they are toggle buttons where the clicked button has to be marked as pressed also.
Upvotes: 2
Reputation: 742
None of the other answers worked for me, but i found something simplier i think :
var button=Ext.get('the_id_div');
button.dom.click();
Upvotes: 0
Reputation: 3085
If you need to execute the "handler" of the button, just run this (tested with ExtJS 4.2)
button.fireHandler()
Upvotes: 1
Reputation: 4480
ExtJS 4.2.1
Ext.get('component-id-of-extjs-button').el.dom.click();
Ext.get('toggle-button2').el.dom.click();
works for me.
Upvotes: 6
Reputation: 21
In case of buttons using handler, you can directly call the function of button.
Considering button
is an Ext JS component, you can use:
button.handler(button);
or if you want to reach a function of event 'click':
button.listeners.click(button);
That would also work to call different button events.
Upvotes: 2
Reputation: 4421
Or if you have an MVC structure you can fire the click event of the button, and if you're listening to the event in your controller and have an associated function it will get called.
button.fireEvent('click', button);
Upvotes: 25
Reputation: 4861
If you want to do this in your test scripts, take a look at my Ext.ux.Test library. If you need it for something other, I would suggest reconsidering your approach.
Upvotes: 0
Reputation: 2630
The last answer on this forum might give you more insight on how you can do that here they are-
1)Create the event code in a function and call the function from both sides: btn.on("clic", ...) and from the code you want to simulate the click.
2)Use: btnView.btnEl.dom.click();
from - http://www.sencha.com/forum/showthread.php?37772-Solved-Programmatically-click-an-Ext.Button
Upvotes: 11