JavaScript for WordPress › Forums › Vanilla JavaScript › why use query selector › Reply To: why use query selector
June 7, 2016 at 7:51 pm
#6109
Zac Gordon
Keymaster
Sure!
With querySelector you can do this:
var el = document.querySelector( '.main-content article h1.post-title' );
The name query comes from how you can write basically any query selector you would do in CSS, including chaining multiple selectors together.
With getElementsByClassName, getElementById and getElementsByTagName it would be a lot more typing and selecting to accomplish the same thing.
However, for a long time that was all we had. I really try not to mention jQuery in the course, but jQuery and others became so popular in large part just because querySelector didn’t exist at the time.
Does that answer the question?