jQuery selectors are actually CSS selectors. So for example, $('#some_HTML_element_id')
will return this element as jQuery object.
What is jQuery object? - A HTML like object which normal Javascript properties, same as - document.getElementBy*()
, but with more functionality related with jQuery. For example:
jQuery('any selector').next() // selecting the next sibling element.
// there is no .next() function.
document.getElementBy*('any selector').next
This is the selection. You just selecting an element, and jQuery adding more functionality. It returning this
element so you can use chaining. For example:
$('.tab').eq(0).next().select()
$('.tab')
-> returning the jQuery set of objects because you are selecting .tab
css class, in most cases, you will have more than one tab.
jQuerySetOfElements.eq(0)
-> returning first element from the set. (in this case the first tab as jQuery object).
jQueryFirstTab.next()
-> selecting next sibling element as jQuery object.
...
- Methods in the $ namespace are generally utility-type methods, and do not work with selections; they are not automatically passed any arguments, and their return value will vary.
This means utility-type function, that are not based on selectors. For example we have $.each()
. As we know Javascript doesn't have foreach loop. So jQuery provide this function for us. See the syntax:
$('selection')
--> returning jQuery elements.
$.UtilityFunction()
--> Do some work for us.
- Methods called on jQuery selections are in the $.fn namespace, and
automatically receive and return the selection as this.
This means that the selected elements with the selector, have the functions in the $.fn
namespace, For example: in the $.fn
namespace we have all the function that helping us to work with elements like: .hide()
, .show()
, .toggle()
. When we select elements with selection, we can use this functions on the selected element, for example: $('#myDiv').hide()
.
$.fn is jQuery.protopype. See this: jQuery fn namespace