If all you need to do is distinguish a string from a jQuery object, and you
want to insure that future revisions of jQuery will never break your code, I
would just test for the string.

I'd also put the logic in a function even if you only use it in one place,
e.g.

    // Return a jQuery object given either a string id or
    // an existing jQuery object or DOM element
    function $fromID( id ) {
        if( typeof id == 'string' ) id = '#' + id;
        return $(id);
    }

And then code:

    var $form = $fromID(form);

-Mike

> From: Eridius
> 
> I am wondering what is the safest way to test if a variable 
> is a jQuery object.  I am building a function where I want to 
> be able to pass in either the id of the form or the form 
> object itself.  The current way I am doing this is
> 
> var $form = (form instanceof jQuery ? form : $('#' + form));
> 
> Is this the safest way that is not likely to change?  Would I 
> be better offer wrapping this functionality into a function 
> all by itself so incase this does change, I only need it to 
> change in one spot (plan on building a complete UI type framework)?
> 

Reply via email to