| Start |
|---|
| Types of Selectors In jQuery you can select elements in a page using many various properties of the element, including:
If you know CSS selectors (opens new window) you will notice selectors in jQuery are the same (with minor exceptions). Take the following HTML for example: Selecting by Type: The following jQuery selector will select all Selecting by Class The following jQuery selector will select all elements of class Selecting by ID The following jQuery selector will select the element with the given ID, which is 2. Selecting by Possession of Attribute The following jQuery selector will select all elements with a defined Selecting by Attribute Value The following jQuery selector will select all elements where the Selecting by Indexed Position (Indexed Selector) The following jQuery selector will select only 1, the second Selecting with Indexed Exclusion To exclude an element by using its index The following selects Selecting with Exclusion To exclude an element from a selection, use The following selects Selecting by Pseudo-state You can also select in jQuery using pseudo-states, including The following jQuery selector will only select the first Combining jQuery selectors You can also increase your specificity by combining multiple jQuery selectors; you can combine any number of them or combine all of them. You can also select multiple classes, attributes and states at the same time. This would select an
You can also separate different selectors with a comma: This would select:
Child and Sibling selection jQuery selectors generally conform to the same conventions as CSS, which allows you to select children and siblings in the same way.
Wildcard selection There might be cases when we want to select all elements but there is not a common property to select upon (class, attribute etc). In that case we can use the |
| Overview Elements can be selected by jQuery using jQuery Selectors (opens new window). The function returns either an element or a list of elements. Basic selectorsRelational operators |
| Caching Selectors Each time you use a selector in jQuery the DOM is searched for elements that match your query. Doing this too often or repeatedly will decrease performance. If you refer to a specific selector more than once you should add it to the cache by assigning it to a variable: This would replace: Caching this selector could prove helpful if your website needs to show/hide this element often. If there are multiple elements with the same selector the variable will become an array of these elements: NOTE: The element has to exist in the DOM at the time of its assignment to a variable. If there is no element in the DOM with a class called Remember to reassign the selector to the variable after adding/removing elements in the DOM with that selector. Note: When caching selectors, many developers will start the variable name with a |
| Combining selectors Consider following DOM Structure Descendant and child selectorsGiven a parent Other combinatorsGroup Selector : ","Select all Multiples selector : "" (no character)Select all Adjacent Sibling Selector : "+"Select all General Sibling Selector : "~"Select all |
| DOM Elements as selectors jQuery accepts a wide variety of parameters, and one of them is an actual DOM element. Passing a DOM element to jQuery will cause the underlying array-like structure of the jQuery object (opens new window) to hold that element. jQuery will detect that the argument is a DOM element by inspecting its nodeType. The most common use of a DOM element is in callbacks, where the current element is passed to the jQuery constructor in order to gain access to the jQuery API. Such as in the |
| HTML strings as selectors jQuery accepts a wide variety of parameters as "selectors", and one of them is an HTML string. Passing an HTML string to jQuery will cause the underlying array-like structure of the jQuery object (opens new window) to hold the resulting constructed HTML. jQuery uses regex to determine if the string being passed to the constructor is an HTMLstring, and also that it must start with The most common use of an HTML string as a selector is when sets of DOM elements need to be created in code only, often this is used by libraries for things like Modal popouts. For example, a function which returned an anchor tag wrapped in a div as a template Would return a jQuery object holding if called as Syntax
RemarksWhen writing
the characters need to be escaped using two backslashes eg. the selectors will be framed like, |
Read Article
https://develop.horje.com/learn/1434/reference