0

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);
    }
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hadi Mostafapour
  • 1,994
  • 2
  • 13
  • 21
  • You want to pass arguments to what function? Please **edit** your question and clarify. – jamesmortensen May 06 '12 at 21:41
  • @jmort253 to $('#div')[q](val); it equal ( $('#div').click(function(pass here){}); ) – Hadi Mostafapour May 06 '12 at 21:45
  • 1
    Below the question, click **edit**, and then put that in your question. Don't force people trying to help you to read code in comments. It shows a lack of respect for the community of people helping you. Thank you! :) – jamesmortensen May 06 '12 at 21:47
  • Thanks for doing that :) – jamesmortensen May 06 '12 at 21:58
  • MR OK - Congrats on finding a solution. However, the answer shouldn't go in the body of the question. Next, you should either mark an accepted answer below, or if none of the ones below solve the problem, you should post your solution as an answer to the question and mark it as the actual, accepted answer. It's perfectly acceptable to answer your own question, as long as it's posted as an actual answer. Good luck, and thanks for coming back to follow up with the solution. You've ensured others can benefit as well! – jamesmortensen May 07 '12 at 00:18

2 Answers2

1

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

Community
  • 1
  • 1
Amit
  • 21,570
  • 27
  • 74
  • 94
  • 6
    Then it's a dupe, not an answer. – Dave Newton May 06 '12 at 21:43
  • @MR.OK: If you can tell us where you want to pass arguments then I can try to give you a proper answer. – Amit May 06 '12 at 21:45
  • You could of course try elaborating on your answer and including some details that make it more than just a few duplicate links. I personally find the two resources you added to be a lot more complicated than what the op is asking, and I'm sure we all could benefit from a short example perhaps? – jamesmortensen May 06 '12 at 21:46
0

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);
    }
});
Hadi Mostafapour
  • 1,994
  • 2
  • 13
  • 21