23

What's the easiest way to find Dom elements with a css selector, without using a library?

function select( selector ) {
 return [ /* some magic here please :) */ ]
};

select('body')[0] // body;

select('.foo' ) // [div,td,div,a]

select('a[rel=ajax]') // [a,a,a,a]

This question is purely academical. I'm interested in learning how this is implemented and what the 'snags' are. What would the expected behavior of this function be? ( return array, or return first Dom element, etc ).

Stefan
  • 9,289
  • 7
  • 38
  • 46
  • The answer you accepted for this question a decade ago is very out of date. It would be worth changing your selection to one that is more suited for today: https://stackoverflow.com/a/4145389/19068 – Quentin Mar 20 '19 at 20:54

5 Answers5

73

In addition to the custom hacks, in recent browsers you can use the native methods defined in the W3C Selectors API Level 1, namely document.querySelector() and document.querySelectorAll():

var cells = document.querySelectorAll("#score > tbody > tr > td:nth-of-type(2)");
VLAZ
  • 26,331
  • 9
  • 49
  • 67
miek
  • 3,446
  • 2
  • 29
  • 31
  • 3
    Docs and browser supports https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll – Michael May 29 '13 at 08:14
  • 4
    This should now be the selected answer - all modern browsers are supported except IE7? In August 2013, that's good enough for me! – cronoklee Aug 02 '13 at 13:46
  • 1
    When using querySelectorAll you will get an array of elements so if you are expecting one element you whould add [0] at the end. Or you can simply use document.querySelector() – Shadi Sep 14 '15 at 08:18
3

These days, doing this kind of stuff without a library is madness. However, I assume you want to learn how this stuff works. I would suggest you look into the source of jQuery or one of the other javascript libraries.

With that in mind, the selector function has to include a lot of if/else/else if or switch case statements in order to handle all the different selectors. Example:

function select( selector ) {
 if(selector.indexOf('.') > 0) //this might be a css class
   return document.getElementsByClassName(selector);
 else if(selector.indexOf('#') > 0) // this might be an id
   return document.getElementById(selector);
 else //this might be a tag name
   return document.getElementsByTagName(selector);
 //this is not taking all the different cases into account, but you get the idea.
};
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
3

Creating a selector engine is no easy task. I would suggest learning from what already exists:

  • Sizzle (Created by Resig, used in jQuery)
  • Peppy (Created by James Donaghue)
  • Sly (Created by Harald Kirschner)
James
  • 109,676
  • 31
  • 162
  • 175
2

Here is a nice snippet i've used some times. Its really small and neat. It has support for the all common css selectors.

http://www.openjs.com/scripts/dom/css_selector/

alexn
  • 57,867
  • 14
  • 111
  • 145
0

No there's no built in way. Essentially, if you decide to go without jQuery, you'll be replicating a buggy version of it in your code.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789