A specific example
$(this) vs this
Inside a click event referencing the clicked objects id
this.id(as you know)
this.value(on most input types. only issues I know are IE when a
valueproperties set on its
<option>elements, or radio inputs in Safari.)
this.classNameto get or set an entire “class” property
<select>to get the selected index
<select>to get a list of
<option>to get its text content
<table>to get a collection of
<tr>to get its cells (td & th)
this.parentNodeto get a direct parent
this.checkedto get the checked state of a
checkboxThanks @Tim Down
this.selectedto get the selected state of an
optionThanks @Tim Down
this.disabledto get the disabled state of an
inputThanks @Tim Down
this.readOnlyto get the readOnly state of an
inputThanks @Tim Down
<a>element to get its
<a>element to get the domain of its
<a>element to get the path of its
<a>element to get the querystring of its
this.srcagainst an element where it is valid to have a
…I think you get the idea.
There will be times when performance is crucial. Like if you’re performing something in a loop many times over, you may want to ditch jQuery.
In general you can replace:
The reason that jQuery is powerful is that it makes some things which are overly tedious in a cross-browser situation (AJAX is one of the best examples) and smooths over the inconsistencies between the myriad of available browsers and provides a consistent API. It also easily facilitates concepts like chaining, implied iteration, etc, to simplify working on groups of elements together.
— Edited to encompass comments —
In my opinion then, the answer depends on what you’re attempting to achieve. If, as I presumed based on your reference to performance benefits, you’re after the best possible speed out of your application, then using jQuery introduces overhead every time you call
There’s a framework called… oh guess what?
Vanilla JS. Hope you get the joke… 😀 It sacrifices code legibility for performance… Comparing it to
jQuery bellow you can see that retrieving a
DOM element by
ID is almost 35X faster. 🙂
Vanilla JS is a fast, lightweight, cross-platform framework for
On their homepage there’s some perf comparisons:
It is perhaps the most comprehensive list of what works and what doesn’t work on what browser anywhere. Pay particular attention to the DOM section. It is a lot to read but the point is not to read it all but to use it as a reference.
When I started seriously writing web apps I printed out all the DOM tables and hung them on the wall so that I know at a glance what is safe to use and what requires hacks. These days I just google something like
quirksmode parentNode compatibility when I have doubts.
Like anything else, judgement is mostly a matter of experience. I wouldn’t really recommend you to read the entire site and memorize all the issues to figure out when to use jQuery and when to use plain JS. Just be aware of the list. It’s easy enough to search. With time you will develop an instinct of when plain JS is preferable.
PS: PPK (the author of the site) also has a very nice book that I do recommend reading
- you know that there is unflinching cross-browser support for what you are doing, and
- it is not significantly more code to type, and
- it is not significantly less readable, and
- you are reasonably confident that jQuery will not choose different implementations based on the browser to achieve better performance, then:
Edit: This answer applies both when choosing to use jQuery overall versus leaving it out, as well as choosing whether to to use vanilla JS inside jQuery. Choosing between
.id leans in favor of JS, while choosing between
.className = .className.replace( new Regexp("(?:^|\\s+)"+foo+"(?:\\s+|$)",'g'), '' ) leans in favor of jQuery.
Others’ answers have focused on the broad question of “jQuery vs. plain JS.” Judging from your OP, I think you were simply wondering when it’s better to use vanilla JS if you’ve already chosen to use jQuery. Your example is a perfect example of when you should use vanilla JS:
Is both slower and (in my opinion) less readable than:
It’s slower because you have to spin up a new JS object just to retrieve the attribute the jQuery way. Now, if you’re going to be using
$(this) to perform other operations, then by all means, store that jQuery object in a variable and operate with that. However, I’ve run into many situations where I just need an attribute from the element (like
Are there any other common practices
operations could be accomplished
easier, without bringing jQuery into
the mix. Or is this a rare case? (of a
jQuery “shortcut” actually requiring
I think the most common case is the one you describe in your post; people wrapping
$(this) in a jQuery object unnecessarily. I see this most often with
value (instead using
Edit: Here‘s an article that explains why using jQuery in the
attr() case is slower. Confession: stole it from the tag wiki, but I think it’s worth mentioning for the question.
Edit again: Given the readability/performance implications of just accessing attributes directly, I’d say a good rule of thumb is probably to try to to use
this.<attributename> when possible. There are probably some instances where this won’t work because of browser inconsistencies, but it’s probably better to try this first and fall back on jQuery if it doesn’t work.