3

I have one function:

function myFunction(){
var id = $(this).attr('id');
}

Now sometimes, myFunction gets called with $(this) as the context but sometimes the context is just 'this'.

In one line how can I do something similar to this:

if(this == $(this)){
var e = $(this);
}

Basically a test to see if 'this' is a jQuery 'this' or a JS 'this'.

Possible?

benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • 1
    It probably won't blow up if you do a `$(this)` either way. – Kos Aug 21 '12 at 15:59
  • 1
    And it doesn't really matter. This is valid `$($($($($($($(this))))))) `. And I'm 99% sure jQuery will check for you and not do the extra work if it's already a jQuery object. – CaffGeek Aug 21 '12 at 15:59
  • Consider having a convention about the type of the context of that function. If you have a jQuery object, you can access the DOM element with `[0]`. – Šime Vidas Aug 21 '12 at 15:59
  • Duplicate of http://stackoverflow.com/questions/1853223/check-if-object-is-a-jquery-object – BradBrening Aug 21 '12 at 16:00
  • and by the way, you are wrong in saying: `Basically a test to see if 'this' is a jQuery 'this' or a JS 'this'`....conceptual error or maybe you might want to word it the other way – Parth Thakkar Aug 21 '12 at 16:00

4 Answers4

12
if (this.jquery) { // refers to jQuery version
  // jQuery object
}

Alternatively:

if (this instanceof jQuery) { // prototype chain
  // jQuery object
}

However, as others have said, it doesn't really matter, $(this) will work whether or not this is already a jQuery object or a DOM element.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • However, I don't think it's worth checking if you're just going to pass it to `$()` anyhow as it will do the check for you. – CaffGeek Aug 21 '12 at 16:01
  • 1
    `var obj = {jquery:1};` TROLOLOLO... `:P` – Šime Vidas Aug 21 '12 at 16:02
  • Would the performance difference between doing João's version, and merely wrapping the 'this' context in jQuery ($thisContext = $(this);) ever be significant? Is there any reason (other than explicitly wanting, or not wanting, a jQuery object) one would prefer one method over another? – Paul Richter Aug 21 '12 at 16:06
  • @Teeg The performance penalty of evaluating the expression `$(this)` is a few micro-seconds (millionths of seconds), which means that it's negligible. You can safely have a few dozens of `$(this)`, `$(window)`, etc. expressions within one function invocation without hurting performance. – Šime Vidas Aug 21 '12 at 19:45
  • @Sime Vidas I had a feeling that would be the case. Joao's answer makes a lot of sense and is good, but for most of the cases I've encountered, I've tended to just wrap $(this). – Paul Richter Aug 21 '12 at 21:48
  • 1
    @Teeg: I agree with you, I've actually added that suggestion in my answer, even though the question asked explicitly for a way to detect a jQuery object. – João Silva Aug 21 '12 at 21:57
3

You can also just do var e = $(this) when this is a jQuery object or if it's not.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
2

One way to deal with it would just be to always wrap it in $(...). Wrapping a jQuery object like that creates a clone (see the jQuery docs), so this HTML:

<a href="test" id="​​​​​​​​​​​​​​​​​​​​​​​​​test-link">Test Link</a>​​​​​​​​​​​​​​​​​​​​​​​

with this JS:

​var link = $('#test-link');
var doubleWrappedLink = $(link);

alert(doubleWrappedLink.attr('href'));​​​​​​​

will correctly pop up "test".

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
0

You can use the following snippet:

if (obj instanceof jQuery || 'jquery' in Object(obj)) { }

Take a look at: Check if object is a jQuery object

Community
  • 1
  • 1
jherax
  • 5,238
  • 5
  • 38
  • 50