have you considered just checking for a jQuery method? seems like that
would be the cleanest, least intrusive way of doing your check, for
example:

 function get_object_from_id(variable)
        {
                // if the param has a "attr" property (which jQuery
objects do,
                // but raw DOM nodes don't), use that. Otherwise,
assume
                // an ID was passed and return the wrapped set
                return variable.attr ? variable : jQuery("#" +
variable);
        };

NOTE: if you passed in a different type of object, and it contained a
"attr" property (or whatever jQuery method used for checking), this
would fall apart. However, if you know the param will either be a raw
DOM node OR a jQuery object, this should work fine.

On Aug 23, 5:53 pm, RyanZec <bas...@gmail.com> wrote:
> Well I think I am going to be going with a function as that was my
> first instinct.
>
> I see where your going with what you have however that would cause
> some issue if say an int or JSON object is passed as it is going to
> return that instead of an expected jQuery object.  The code I end up
> having is:
>
>         function get_object_from_id(variable)
>         {
>                 if(typeof variable == 'string')
>                 {
>                         variable = $('#' + variable);
>                 }
>                 else if(variable instanceof jQuery)
>                 {
>                         //already a jQuery object so we need to do nothing
>                 }
>                 else
>                 {
>                         //error code
>                 }
>
>                 return variable;
>         };
>
> Not sure how to handle the error (whether to throw an exception or
> just return null or what not) but this way I can handle it if
> something else is passed besides a string or jQuery object.
>
> And even tho I doubt the jQuery object are going to stop being an
> instance of jQuery any time soon, if it does, I have one location to
> change my code.
>
> Thanks for you advance.
>
> On Aug 23, 4:30 pm, "Michael Geary" <m...@mg.to> wrote:
>
> > 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