What is a selector engine?
Asked Answered
C

5

32

I've seen news of John Resig's fast new selector engine named Sizzle pop up in quite a few places, but I don't know what a selector engine is, nor have any of the articles given an explanation of what it is. I know Resig is the creator of jQuery, and that Sizzle is something in Javascript, but beyond that I don't know what it is. So, what is a selector engine?

Thanks!

Chambless answered 25/8, 2008 at 16:59 Comment(0)
T
52

A selector engine is used to query a page's DOM for particular elements, based on some sort of query (usually CSS syntax or similar).

For example, this jQuery:

$('div')

Would search for and return all of the <div> elements on the page. It uses jQuery's selector engine to do that.

Optimizing the selector engine is a big deal because almost every operation you perform with these frameworks is based on some sort of DOM query.

Tertial answered 25/8, 2008 at 17:3 Comment(0)
G
19

A selector engine is a JavaScript library that lets you select elements in the DOM tree using some kind of string for identifying them (think regular expressions for DOM elements). Most selector engines use some variation of the CSS3 selectors syntax so, for example, you can write something like:

var paragraphs = selectorengine.select('p.firstParagraph')

to select all P elements in the document with class firstParagraph.

Some selector engines also support a partial implementation of XPath, and even some custom syntaxes. For example, jQuery lets you write:

var checkedBoxes = jQuery('form#login input:checked')

To select all checked check boxes in the login form in the document.

Gowrie answered 25/8, 2008 at 17:7 Comment(1)
I guess, you just need to include the "js" file in your page. Do you need JQuery or some other library to use it?Former
R
9

A selector engine is a way to traverse the DOM looking for a specific element.

An example of a built in selector engine:

var foo = document.getElementById('foo');
Ryon answered 25/8, 2008 at 17:7 Comment(1)
thanks for nice example built-in selector engine I read some answers and still ask myself, getElementById is a selector engine or not (it looks like, but too obvious so no one mentions :D)Disobey
K
5

Also, Sizzle is the engine John Resig is working on currently to replace jQuery's already fantastic selector engine.

Kite answered 25/8, 2008 at 17:7 Comment(0)
V
2

A selector engine is used to find elements in a document, in the same way as CSS stylesheets does. Currently only Safari has the built-in querySelectorAll function which does just that. With other browser you have to use external JavaScript implementations as LlamaLab Selector or Sizzle instead.

Volution answered 11/2, 2009 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.