The $() Factory Function
No matter which type of selector we want to use in jQuery—be it CSS, XPath, or custom—we always start with the dollar sign and parentheses: $()
As mentioned in Chapter 1, the $()
function removes the need to do a for
loop to access a group of elements since whatever we put inside the parentheses will be looped through automatically and stored as a jQuery object. We can put just about anything inside the parentheses of the $()
function. A few of the more common examples include:
A tag name: .
$('p')
gets all paragraphs in the document.An ID: .
$('#some-id')
gets the single element in the document that has the correspondingsome-id
ID.A class: .
$('.some-class')
gets all elements in the document that have a class ofsome-class
.
Note
Making jQuery Play Well with Other JavaScript Libraries
In jQuery, the dollar sign $
is simply shorthand for jQuery
. Because a $()
function is very common in JavaScript libraries, conflicts could arise if more than one of these libraries...