View Full Version : [CLOSED] ext:Button: Adding listener to a click event

Apr 04, 2012, 5:59 PM

I'm creating my ext:window as javascript: It is working.
But, I wish pass the buttons of window as parameter of the function;
I'm creating a function below to create a button. But my problem is: I want set the javascript that be executed when the button is clicked.

In vb.Net, i do this:

function createButton(text as string, fn as string)
dim btn = New Ext.Net.Button
btn.Id = "btnOk"
btn.Text = text
btn.Listeners.Click.Handler = fn
return btn
end function

How I do it in Javascript?

function createButton(text, fn) {
var btn;

btn = Ext.create("Ext.button.Button", {
id: "btn" + text,
text: text
btn.on("click", "function () {" + fn + "}");
return btn;

Apr 04, 2012, 11:43 PM

The best way to figure this out is to configure in code-behind, then view the Page in a browser, then View > Source to see the client-side configuration script generated.

You can then take that script and pull out the piece you required.

Hope this helps.

Apr 05, 2012, 11:35 AM
Hi Geoffrey... thanks for your suggestion...

Daniil gave me this tip before, and I have used enough.

But, in this case, The function in listener click not static... I wish pass this function as parameter, because, depending the window where I am, I want a different action on the OK button click of window. My window is created in Javascript code too.

My problem is transform a string with javascript function passed as parameter in a function associated to click listener.

In codebehind, it's easy:

btn.Listeners.Click.Handler = fn

Where 'fn' is a string with javascript function.

But, I don't know how I do it in Javascript.

I'm trying this way (in Javascript), but, don't works:

btn.on("click", "function () {" + fn + "}");

Apr 05, 2012, 11:54 AM
In JavaScript I would suggest to pass a function instance as a parameter.

Then it will look this way:

function createButton (text, fnParam) {
var btn;

btn = Ext.create("Ext.button.Button", {
id : "btn" + text,
text : text,
listeners : {
click : {
fn : fnParam
return btn;

Apr 05, 2012, 11:59 AM
In your suggestion, the 'fnParam' is a javascript function or a string contains java script function code?

Apr 05, 2012, 12:00 PM
A JavaScript function.

It's my suggestion - pass a JavaScript function as a parameter.

Apr 05, 2012, 12:04 PM

Thanks a lot for your help!

I pass the javascript function as parameter and works very fine... I will try now use this in my real source code...

If you want, you can close this thread... If I have new difficults, I create a new thread.

Thanks a lot, again