> > $.fn.checkform = function() {
> >           $(this).bind("click", function(){alert("it works");}  );
> > };
> > 
> > What am I doing wrong ?

> Ok I got it:
> 
> $.fn.checkform = function() {  
>    return this.each(function(){      
>           $(this).bind("click", function(){alert("it works");}  );   
>    });   
> };

You must be running an old version of jQuery. Your original code works in
the current release, but it wouldn't work in older versions.

Inside your checkform function, "this" is a jQuery object, not a DOM
element. So you don't need to wrap it in $(this) - although newer versions
of the jQuery code allow that. Nor do you need to run .each() on it and then
re-wrap each of the resulting DOM elements in their own $(this) wrappers -
although that works too.

Instead, you can simply code:

   $.fn.checkform = function() {
      this.bind("click", function(){alert("it works");}  );
   };

It is really confusing that "this" means different things at different times
- in a plugin function it's a jQuery object, inside a .each() callback it's
a DOM element - so it's always good to check whether "this" is what you
expect it to be. Set a breakpoint and look at "this" in the debugger, or use
an alert( this.toSource() ) in Firefox to find out what "this" really is.

-Mike


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to