10

I want to create function that can be used either with Id or by passing jQuery object.

var $myVar = $('#myId');

myFunc($myVar);
myFunc('myId');

function myFunc(value)
{
    // check if value is jQuery or string
}

How can I detect what kind of argument was passed to the function?

Note! This question is not same. I don't want to pass selector string like #id.myClass. I want to pass the jQuery object like in the example.

3
  • 4
    A jQuery object is not a "jQuery selector"; it's an object. A selector is a string with CSS selector syntax. Commented May 23, 2012 at 14:00
  • If you want to detect a jQuery object, then the part of that post that says value instanceof of jQuery (le mans: detecting an instance of jQuery object) would be the answer. Commented May 23, 2012 at 14:01
  • 2
    Off the top of my head, just check if the variable is a string or an object? Commented May 23, 2012 at 14:01

7 Answers 7

18

Use the typeof operator

if ( typeof value === 'string' ) {
  // it's a string
} else {
  // it's something else
}

Or to make really sure it's an instance of the jQuery object

if ( typeof value === 'string' ) {
  // it's a string
} else if ( value instanceof $) {
  // it's a jQuery object
} else {
  // something unwanted
}
Sign up to request clarification or add additional context in comments.

2 Comments

At least quick tests show that 'value instanceof $' is working smoothly. This was the thing that I was looking for. Thanks!
I would promote the second part as that really does the trick excluding other vanilla objects.
3

Every jquery object has a property jquery. This will fail, of course, if your object has jquery property...but you can have stricter checking if you want...

function(o) {
    if(typeof o == 'object' && o.jquery) // it's jquery object.
}

Comments

1
function myFunc(value)
{
   if (typeof value == "string") {
      //it's a string
   }
   else if (value != null && typeof value == "object"} {
      //it's an object (presumably jQuery object)
   }
   else {
      //it's null or something else
   }


}

2 Comments

typeof value will never be null. Did you mean value != null?
@JamesAllardice, yep, that's what I meant. Fixed it. Thank you.
0

Would it not be sufficient to check the type of the argument?

function myfunc(arg)
{
    if(typeof arg == 'string')
    {

    }
    else if(typeof arg == 'object')
    {

    }  
}

Check this Fiddle.

1 Comment

This doesn't consider null, which also has typeof = "object".
0

Try this:

function myFunc(value)
{
   if(typeof value === 'object') {
   }
   else{
   }
}

Comments

0

Try using typeof, eg:

var $myVar = $('#myId');

myFunc($myVar);
myFunc('myId');

function myFunc( value ){
    // check if value is jQuery or string
    switch( typeof value ) {
        case 'object':
        // is object
        break;

        case 'string':
        // is string
        break;

        // etc etc.
    }
}

Comments

0
function myFunc(value)
{
  if(typeof(value) == 'string')
    //this is a string
  else if (value === jQuery)
    //this is jQuery
  else if (typeof(value) == 'object')
    //this is an object
}

NOTE: did this in the console:

> jQuery
function (a,b){return new e.fn.init(a,b,h)}
> var value = jQuery
undefined
> value
function (a,b){return new e.fn.init(a,b,h)}
> value === jQuery
true

3 Comments

value === jQuery gives false for jQuery object
value === jQuery will only be true for jQuery itself, not instances of it. You want the instanceof operator. @RGB - Note that typeof is an operator, not a function, so you don't need the parentheses (they won't cause any problems, but it can be clearer without them).
uhh, yes it does.. :P but i see you have a solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.