Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery Selectors

Posted in jQuery Tutorial
Updated on May 19, 2024
By Mari Selvan
👁️ 37 - Views
⏳ 4 mins
💬 0
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:

FunctionsExplanation
*Selects all elements.
:animatedSelect 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.
:buttonSelects all button elements and elements of type button.
:checkboxSelects all elements of type checkbox.
:checkedMatches 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.
:disabledSelects all elements that are disabled.
(“element”)Selects all elements with the given tag name.
:emptySelect all elements that have no children (including text nodes).
:enabledSelects all elements that are enabled.
:eq()Select the element at index n within the matched set.
:evenSelects even elements, zero-indexed. See also :odd.
:fileSelects all elements of type file.
:first-childSelects all elements that are the first child of their parent.
:first-of-typeSelects all elements that are the first among siblings of the same element name.
:firstSelects the first matched DOM element.
:focusSelects 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.
:headerSelects all elements that are headers, like h1, h2, h3 and so on.
:hiddenSelects all elements that are hidden.
(“#id”)Selects a single element with the given id attribute.
:imageSelects all elements of type image.
:inputSelects all input, textarea, select and button elements.
:lang()Selects all elements of the specified language.
:last-childSelects all elements that are the last child of their parent.
:last-of-typeSelects all elements that are the last among siblings of the same element name.
:lastSelects 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.
:oddSelects odd elements, zero-indexed. See also :even.
:only-childSelects all elements that are the only child of their parent.
:only-of-typeSelects all elements that have no siblings with the same element name.
:parentSelect all elements that have at least one child node (either an element or text).
:passwordSelects all elements of type password.
:radioSelects all elements of type radio.
:resetSelects all elements of type reset.
:rootSelects the element that is the root of the document.
:selectedSelects all elements that are selected.
:submitSelects all elements of type submit.
:targetSelects the target element indicated by the fragment identifier of the document’s URI.
:textSelects all input elements of type text.
:visibleSelects 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy