Sunday, June 28, 2009

[jQuery] Re: Passing a jquery object as a function parameter

Passing a jQuery object as a function parameter is no problem. It looks like you've got a bug in your selector. Change

  doStuff($('myForm'));

to

  doStuff($('#myForm'));

Even though you said your later code works, seems you have the same issue there as well, as you're doing

  function doStuff(theform) {
     $(theform).show();
  }
  doStuff('myForm');

instead of

  function doStuff(theformId) {
     $('#' + theformId).show();
  }
  doStuff('myForm');

or

  function doStuff(theform) {
     $(theform).show();
  }
  doStuff('#myForm');

- Richard

On Sun, Jun 28, 2009 at 12:47 AM, ferdjuan <afred3@gmail.com> wrote:

I had a script that was acting unexpectedly because of a function
parameter. I was trying to pass a jquery object through a function to
attach some events. I was wondering if someone could tell me why this
didn't fly:

function doStuff(theform) {
   theform.show();
}

doStuff($('myForm'));

The fix was to pass the id as a string:

function doStuff(theform) {
   $(theform).show();
}

doStuff('myForm');

Is there a reason why I couldn't pass the object directly through?
Thanks

Thanks

No comments: