I’m writing a plugin for jQuery and I want to make it so the user can pass data to the plugin in any form. I have the JSON or array problem worked out, but I’m having trouble trying to determine if the data is a jQuery object.
data = $('#list li');
console.debug( $.isPlainObject(data) ); // false
console.debug( $.isArray(data) ); // false
console.debug( data[0].tagName == "LI" ); // true, but see note below
The last method returns true, but there is no guarantee that the user is using an LI tag for their data, so I think I need something like this:
if ( $.isjQueryObject(data) ) { /* do something */ }
Does anyone know a better method?
The
jQueryobject (or its alias$) is a plain constructor function, all jQuery objects inherit from thejQuery.prototypeobject (or its aliasjQuery.fn).You can check if an object exists in the prototype chain of other object, by using either the
instanceofoperator or theisPrototypeOfmethod, for example:Or: