Frederick M. Rogers
Frederick M. Rogers

Reputation: 921

jQuery - Naming convention, selector or element?

This may be the dumbest question ever asked but i can't find a definitive answer and it is driving me insane.

Are ALL selectors passed to the jQuery constructor considered an element ?

DOM elements such as HTML, HEAD, BODY, DIV, SECTION, A, P ect. are all elements of the Document Object Model.

What about selectors such as window and document are they also elements?

I ask this silly question because when it comes to naming convention i often see jQuery selectors refered to as elements or passed as function arguments using as el, element or even elem.

Thanks!

Upvotes: 0

Views: 1031

Answers (1)

Barmar
Barmar

Reputation: 780798

An element is a node in the DOM, or the Javascript object that represents a node.

A selector is a string that's used to specify which elements to find in the DOM. jQuery selectors are similar to CSS selectors. Selectors include tag names like div, ID selectors like #container, class selectors like .myclass, attribute selectors like [name=username], pseudo-selectors like :checked, and lots of combinations of these.

The jQuery() function can accept either an element or a selector as an argument, and will return a jQuery collection object that contains the specified elements. If you give it an element, it simply wraps it in a jQuery object; if you give it a selector, it searches the DOM for the matching elements, and wraps them in a jQuery object.

In the code below, jquery and jquery2 are effectively equivalent:

var element = document.getElementById("someid");
var jquery1 = $(element);

var selector = "#someid";
var jquery2 = $(selector);

The jQuery() function can also accept other kinds of input that aren't directly relevant to your question.

Upvotes: 1

Related Questions