Deniz Beker
Deniz Beker

Reputation: 2174

Assign Javascript UI widget function to a parameter and execute

Even though there are similar questions, I couldn't find the solution of this case. In javascript, I have two ui widgets (let's call textbox1 and textbox2). My purpose is to write a generic handler for those functionalities. For example instead of doing following code,

initialize: function(element, options){

    if(options.isTextbox1){
       return element.textbox1(options);
    }
    return element.textbox2(options);

}

I would like to do as following,

var mode;
initialize: function(element, options){
    if(element.isTextbox1){
        mode = element.textbox1;
    }
    else{
        mode = element.textbox2;
    }
    return mode(options);
}

However, the second code piece is throwing exception as

Uncaught TypeError: this.each is not a function

thrown from jquery-ui-1.9.2.

What would be the appopriate way to handle this situation?

Upvotes: 1

Views: 28

Answers (1)

Deniz Beker
Deniz Beker

Reputation: 2174

I was able to solve it.

Here is the usage

var mode;
initialize: function(element, options){
    if(element.isTextbox1){
        mode = 'textbox1';
    }
    else{
        mode = 'textbox2';
    }
    return element[mode](options);
}

Upvotes: 1

Related Questions