jQuery Basic
jQuery Selectors
- jQuery Selectors
- jQuery *
- jQuery :animated
- jQuery [name|=”value”]
- jQuery [name*=”value”]
- jQuery [name~=”value”]
- jQuery [name$=”value”]
- jQuery [name=”value”]
- jQuery [name!=”value”]
- jQuery [name^=”value”]
- jQuery :button
- jQuery :checkbox
- jQuery :checked
- jQuery Child Selector
- jQuery .class
- jQuery :contains()
- jQuery Descendant Selector
- jQuery :disabled
- jQuery Element
- jQuery :empty
- jQuery :enabled
- jQuery :eq()
- jQuery :even
- jQuery :file
- jQuery :first-child
- jQuery :first-of-type
- jQuery :first
- jQuery :focus
- jQuery :gt()
- jQuery Has Attribute
- jQuery :has()
- jQuery :header
- jQuery :hidden
- jQuery #id
- jQuery :image
- jQuery :input
- jQuery :lang()
- jQuery :last-child
- jQuery :last-of-type
- jQuery :last
- jQuery :lt()
- jQuery [name=”value”][name2=”value2″]
- jQuery (“selector1, selector2, selectorN”)
- jQuery (“prev + next”)
- jQuery (“prev ~ siblings”)
- jQuery :not()
- jQuery :nth-child()
- jQuery :nth-last-child()
- jQuery :nth-last-of-type()
- jQuery :nth-of-type()
- jQuery :odd
- jQuery :only-child
- jQuery :only-of-type
- jQuery :parent
- jQuery :password
- jQuery :radio
- jQuery :reset
- jQuery :root
- jQuery :selected
- jQuery :submit
- jQuery :target
- jQuery :text
- jQuery :visible
jQuery Selectors
Photo Credit to CodeToFun
🙋 Introduction
Discover the versatility and power of jQuery Selectors, enabling efficient traversal and manipulation of HTML elements in JavaScript. Learn how to target specific elements within a document, facilitating dynamic interactions and seamless user experiences.
🤔 What are jQuery Selectors?
jQuery Selectors are expressions that allow you to identify and select HTML elements based on their attributes, properties, and relationships with other elements. They provide a concise and powerful syntax for accessing elements within a document, enabling developers to manipulate the DOM with ease.
🗝️ Key Features
- Element Selection: jQuery Selectors offer various methods for selecting elements based on their tag names, classes, IDs, attributes, and more.
- DOM Traversal: Navigate through the DOM tree using jQuery Selectors to access parent, child, sibling, and descendant elements efficiently.
- Dynamic Filtering: Filter elements based on specific criteria such as visibility, presence of certain attributes, or position within the document.
📝 List of Selectors
The following are the list of Selectors:
Functions | Explanation |
---|---|
* | Selects all elements. |
:animated | Select all elements that are in the progress of an animation at the time the selector is run. |
[name|=”value”] | Selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-). |
[name*=”value”] | Selects elements that have the specified attribute with a value containing a given substring. |
[name~=”value”] | Selects elements that have the specified attribute with a value containing a given word, delimited by spaces. |
[name$=”value”] | Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive. |
[name=”value”] | Selects elements that have the specified attribute with a value exactly equal to a certain value. |
[name!=”value”] | Select elements that either don’t have the specified attribute, or do have the specified attribute but not with a certain value. |
[name^=”value”] | Selects elements that have the specified attribute with a value beginning exactly with a given string. |
:button | Selects all button elements and elements of type button. |
:checkbox | Selects all elements of type checkbox. |
:checked | Matches all elements that are checked or selected. |
(“parent > child”) | Selects all direct child elements specified by “child” of elements specified by “parent”. |
(“.class”) | Selects all elements with the given class. |
:contains() | Select all elements that contain the specified text. |
(“ancestor descendant”) | Selects all elements that are descendants of a given ancestor. |
:disabled | Selects all elements that are disabled. |
(“element”) | Selects all elements with the given tag name. |
:empty | Select all elements that have no children (including text nodes). |
:enabled | Selects all elements that are enabled. |
:eq() | Select the element at index n within the matched set. |
:even | Selects even elements, zero-indexed. See also :odd. |
:file | Selects all elements of type file. |
:first-child | Selects all elements that are the first child of their parent. |
:first-of-type | Selects all elements that are the first among siblings of the same element name. |
:first | Selects the first matched DOM element. |
:focus | Selects element if it is currently focused. |
:gt() | Select all elements at an index greater than index within the matched set. |
[name] | Selects elements that have the specified attribute, with any value. |
:has() | Selects elements which contain at least one element that matches the specified selector. |
:header | Selects all elements that are headers, like h1, h2, h3 and so on. |
:hidden | Selects all elements that are hidden. |
(“#id”) | Selects a single element with the given id attribute. |
:image | Selects all elements of type image. |
:input | Selects all input, textarea, select and button elements. |
:lang() | Selects all elements of the specified language. |
:last-child | Selects all elements that are the last child of their parent. |
:last-of-type | Selects all elements that are the last among siblings of the same element name. |
:last | Selects the last matched element. |
:lt() | Select all elements at an index less than index within the matched set. |
[name=”value”][name2=”value2″] | Matches elements that match all of the specified attribute filters. |
(“selector1, selector2, selectorN”) | Selects the combined results of all the specified selectors. |
(“prev + next”) | Selects all next elements matching “next” that are immediately preceded by a sibling “prev”. |
(“prev ~ siblings”) | Selects all sibling elements that follow after the “prev” element, have the same parent, and match the filtering “siblings” selector. |
:not() | Selects all elements that do not match the given selector. |
:nth-child() | Selects all elements that are the nth-child of their parent. |
:nth-last-child() | Selects all elements that are the nth-child of their parent, counting from the last element to the first. |
:nth-last-of-type() | Selects all the elements that are the nth-child of their parent in relation to siblings with the same element name, counting from the last element to the first. |
:nth-of-type() | Selects all elements that are the nth child of their parent in relation to siblings with the same element name. |
:odd | Selects odd elements, zero-indexed. See also :even. |
:only-child | Selects all elements that are the only child of their parent. |
:only-of-type | Selects all elements that have no siblings with the same element name. |
:parent | Select all elements that have at least one child node (either an element or text). |
:password | Selects all elements of type password. |
:radio | Selects all elements of type radio. |
:reset | Selects all elements of type reset. |
:root | Selects the element that is the root of the document. |
:selected | Selects all elements that are selected. |
:submit | Selects all elements of type submit. |
:target | Selects the target element indicated by the fragment identifier of the document’s URI. |
:text | Selects all input elements of type text. |
:visible | Selects all elements that are visible. |
📚 Use Cases
- Event Handling: Attach event listeners to specific elements on a web page to trigger actions such as clicks, hovers, or form submissions.
- Dynamic Content: Dynamically update or manipulate content based on user interactions or server responses.
- Form Validation: Validate form input fields and provide feedback to users using jQuery Selectors to target form elements.
🌟 Advantages
- Ease of Use: jQuery Selectors provide a simple and intuitive syntax for accessing and manipulating DOM elements, reducing the amount of boilerplate code required.
- Cross-Browser Compatibility: jQuery handles browser quirks and inconsistencies, ensuring consistent behavior across different browsers.
- Performance: jQuery Selectors are optimized for performance, making DOM manipulation faster and more efficient.
🎉 Conclusion
jQuery Selectors are a fundamental aspect of jQuery that empowers developers to interact with and manipulate HTML elements with ease. Whether you're selecting elements for styling, animation, or event handling, mastering jQuery Selectors is essential for building dynamic and responsive web applications.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (jQuery Selectors), please comment here. I will help you immediately.