Ronald
Ronald

Reputation: 2927

How to pass variable to javascript function through cq.ext.button handler?

I have the following code in my CQ dialog.xml

<toolbar
jcr:primaryType="cq:Widget"
xtype="toolbar">
<items
    jcr:primaryType="cq:WidgetCollection">
    <input
        jcr:primaryType="cq:Widget"
        xtype="textfield"
        name="./myInput">
    </input>
    <button
        jcr:primaryType="cq:Widget"
        xtype="button"
        name="./myButton"
        text="Submit" handler ="function() {passMyInput()};">
    </button>
</items>

I have implemented the function passMyInput() as follow:

passMyInput(){ alert("test");}

This works fine. My question is how to pass the value of ./myInput to the function passMyInput ?

I have tried handler ="function() passMyInput('./myInput')};" but it doesn't work

Upvotes: 0

Views: 869

Answers (1)

rakhi4110
rakhi4110

Reputation: 9281

The button's handler function receves 2 arguments, button b and eventObject e.

We can obtain the container dialog through the button and then use the getField() method to obtain the value of the field.

The modified hander function would be

function(b, e) {
    var dlg = b.findParentByType('dialog');
    var val = dlg.getField('./myInput').getValue();
    passMyInput(val);
}

For more info, refer widget docs

Upvotes: 1

Related Questions