Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I was at an interview recently and although they knew that I was a beginner in javascript they asked me what selectors I used before? I didn't know what to say.

I came back home and searched for it but can't find a clear definition. Isn't a selector something like a .class or #id?

share|improve this question

closed as off topic by Kilian Foth, Glenn Nelson, Dynamic, MainMa, Martijn Pieters Mar 6 '13 at 12:00

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

There are no "JavaScript selectors". – ThiefMaster Mar 5 '13 at 15:08

2 Answers 2

up vote 15 down vote accepted

Yes, you were interviewed by morons. Consider that bullet dodged. They were probably thinking of jQuery, which you probably should learn, but it does in fact use CSS selectors and xpath... (whatever you call xpath statements/selector thingies) for syntax. The right answer if you should want to be hired by somebody who makes the same mistake again is "JavaScript doesn't have selectors. Are you talking about jQuery? Or are you referring to the DOM API?" The DOM API, btw, is something you should already know if you don't.

Edit: Okay, maybe not total morons. They could have been thinking of the query selector API (see Dystroy's answer) but IMO, that still should have been clear.

share|improve this answer
lol at silent down-vote. Maybe somebody who's asked that question? – Erik Reppen Mar 5 '13 at 15:07
@AAA: w3schools is a horrible reference. Please do not use it for anything! – ThiefMaster Mar 5 '13 at 15:08
The official jQuery docs: – ThiefMaster Mar 5 '13 at 15:10
sigh w3schools is fine for beginners w3fools isnt even kept up to date as is, and is pretty pretentious. However MDN is a much better resource. – Loktar Mar 5 '13 at 15:10
@Loktar I would say w3schools is fine for reference if it happens to have the exact method you were looking for on a google but I wouldn't advise treating it as a primary source for beginners since it tends to be dated/incomplete. On the other hand, unlike MDN which does have good info, at least the info is always there and not awaiting some wiki upgrade or other form of content-blocking foolishness. – Erik Reppen Mar 5 '13 at 15:12

Your interviewer was probably referring to the query language which lets you form selectors to find elements in the DOM using methods like querySelectorAll.

Note that selectors are normalized by the in the selectors API :

Selectors, which are widely used in CSS, are patterns that match against elements in a tree structure [SELECT][CSS21]. The Selectors API specification defines methods for retrieving Element nodes from the DOM by matching against a group of selectors. It is often desirable to perform DOM operations on a specific set of elements in a document. These methods simplify the process of acquiring specific elements, especially compared with the more verbose techniques defined and used in the past.

The purpose of the question could be to check you know those selectors exist, which would make a difference with many developers only aware of the use of libraries like jQuery and who would make the mistake of answering that JavaScript doesn't have selectors.

share|improve this answer
That's my guess too, the new(ish) Selectors API is probably what they meant. – Yannis Mar 5 '13 at 15:12
Good point. But I guess I still think of CSS as having selectors and JS/JQ as just borrowing the syntax. If they'd asked about selector methods, it would be a lot more clear. – Erik Reppen Mar 5 '13 at 15:33
The API is called the selectors API. That's clear to me. – Denys Séguret Mar 5 '13 at 15:35
Still a really stupid interview question, IMO. – Erik Reppen Mar 5 '13 at 15:40
On that I may agree. Many good developers could very well miss what was meant by the question. – Denys Séguret Mar 5 '13 at 15:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.