Is it possible to reverse jquery '$(this)' into javascript 'this'?
How can this be done?
Is it possible to reverse jquery '$(this)' into javascript 'this'?
How can this be done?
jQuery is set based, and lets you access the elements of that set using either indexer notation ([]
) or the get
method.
So if you have a jQuery set:
var set = $(this);
...you can access its elements via set[0]
through set[set.length - 1]
and via set.get(0)
through set.get(set.length - 1)
(get
also supports negative indexes for indexing from the end).
In the case of $(this)
, of course, you don't need to use $()
at all — just use this
directly. This is true whenever you have a raw DOM reference, whether this
or event.target
or whatever.
jQuery
or $
is primarily just a wrapper function that returns a jQuery object. So removing the $
is all you need to get the normal DOM element this
.
this // DOM ELement
$(this) // jQuery object
this // Again just a DOM Element
You need not do anything more. this
will always be the primitive DOM Element, it's because you're wrapping it, that it returns you a jQuery object.
jQuery offers .get
which does the same too, but offers the convenience of getting a specific element if this
is a collection.