0

Is it possible to reverse jquery '$(this)' into javascript 'this'?

How can this be done?

2
  • 4
    $(this)[0] or $(this).get(0) Commented Oct 6, 2015 at 13:39
  • 1
    Um, just use this to start? Commented Oct 6, 2015 at 13:40

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.