Hadi Mostafapour
Hadi Mostafapour

Reputation: 2276

How to pass argument?

Solved, Yohoo

I have a dialog plugin, like this

$("#dialog").dialog({
  click:function(){
    alert(1);
  },
  'class':"dialog"
});

Following code is a chunk of main code that loop on options and check if key is a jQuery function and then call it else set it as attribute

$.each(options,function(key,val){
   if(key in $.attrFn){
    $('#div')[key](val);    // I want pass arguments to this function
    // equal $('#div').click(function(args){
    //     alert(1);
    // });
    // this is like jQuery ui dialog buttons options
   } else {
    $('#div').attr(key,val);
   }
});

I want pass some arguments to the function, but I don't know how??

Example:

$("#dialog").dialog({
  click:function(dialog){
    dialog.disAppear();
  },
  'class':"dialog"
});

Solved:

$.each(v,function(q,w){
    if(q in $.attrFn){
            //console.log(dialog);
            b[q](function(){
                w(dialog);
            });
    } else {
        b.attr(q,w);
    }
});

Upvotes: 0

Views: 172

Answers (2)

Hadi Mostafapour
Hadi Mostafapour

Reputation: 2276

we Can use JavaScript IIFE to pass arguments, so we can: i wrapped it to function(){}, to prevent self execution

$.each(v,function(q,w){
    if(q in $.attrFn){
        if($.isFunction(w)) {
            b[q](function(e){
                w(e,dialog);
            });
        } else b[q](w);
    } else {
        b.attr(q,w);
    }
});

Upvotes: 0

Amit
Amit

Reputation: 22086

Here is a working example of Dialog on http://jsfiddle.net/Jams/hcTTH/

There is a question on SO which may suits your requirements here it is How to pass a parameter to jQuery UI dialog event handler?

Other one is here

Passing data to a jQuery UI Dialog

Upvotes: 1

Related Questions