views:

154

answers:

4

Is there a way to get JavaScript this from jQuery this?

+11  A: 

this == this, whatever this is.

this is a not jquery it is a special, somewhat convoluted, javascript keyword that describes the current scope of execution.

your challenge may be determining or controlling what this is.

Sky Sanders
+1 for the irony of it all.
Gabriel
I think that the OP actually wants to know something else
jAndy
@jAndy - ok, i'll bite...
Sky Sanders
+6  A: 

There is:

$('a').click(function(){
   var jqueryobject = $(this);
   var domelement   = this;
}); 

Within such a closure, this always represent the native DOM element which must/can be wrapped into a jQuery object.

If you already got a jQuery object and need to get the DOM element either use

var DOMelement = $(this)[0];

or

var DOMelement = $(this).get(0);

Since jQuery objects are array like objects you can always grab them with standard array access [] notation. The jQuery method .get() will actually do the same. With both ways, you'll receive the DOM element at that array position.

General - what is this ?

  • this contains a reference to the object of invocation
  • this allows a method to know what object it is concerned with
  • this allows a single function object to service many functions
  • So this is the most important part of all protoypal inheritance things
jAndy
but is a memeber of the jquery `this`? `that` is the question.
Sky Sanders
As an aside, I _really_ don't like using the array accessor to get to the DOM element. Totally unintuitive.
kibibu
@code poet: `this` is a native javascript keyword, actually always holding the scope of a javascript `block`. So no, `this` is no jQuery thing.
jAndy
whoa, hold on, a member of the jquery *could* be `this` but is it? that is my point. OP question is setting us up to play 'who's on first'
Sky Sanders
If "this" is already a jquery object, and you need to get the DOM element, you do NOT want to do: $(this)[0]. Yes, it works, but you're doing extra work. "this" is ALREADY a jquery object. Don't do $(this). Just do this[0] or this.get(0). No need to jquery a jquery.
Samuel Meacham
@Samuel: not in an event handler. Within a jQuery event handler `this` represents the `DOM element`, see http://jsbin.com/ogofi3/2/edit
jAndy
@jAndy: I realize that's how event handlers work in jquery. But your statement said "If you already got a jQuery object and need to get the DOM element", and then said to use $(this), which is incorrect. It works, but it's incorrect.
Samuel Meacham
jAndy, arguing with @sam, except in *rare* occasions, is an exercise in edutainment. go ahead and play the game but don't fool yourself into thinking you are going to win. *most* of the time, that is. ;-) *yes, you may quote me on that*
Sky Sanders
A: 

Try to access this (DOM element) instead of $(this) (jquery object).

Krunal
+1  A: 

I don't really understand what you're asking for here, but I'll give it a shot. Firstly, remember that jQuery is just a series of functions written in Javascript. The this keyword is literally the same in "both" contexts.

Perhaps you want to get the actual DOM element instead of the jQuery object? You can use .get():

// let's say there's a single paragraph element
var obj = $('p');   // obj is a jQuery object 
var dom = obj.get(0);  // dom is a DOM element
nickf