JQuery Selectors
JQuery Selectors
jQuery is a popular JavaScript library that simplifies HTML document traversal, manipulation, and event handling. It provides a wide range of powerful selectors that allow you to target and manipulate elements in the DOM (Document Object Model). Here are some commonly used jQuery selectors:
Element Selector: Selects all elements with a given tag name. Example:
$('p')
selects all<p>
elements.Class Selector: Selects all elements with a given class. Example:
$('.myClass')
selects all elements with the class “myClass”.ID Selector: Selects a single element with a given ID. Example:
$('#myId')
selects the element with the ID “myId”.Attribute Selector: Selects elements based on their attribute values. Example:
$('[data-name="John"]')
selects elements with the attributedata-name
equal to “John”.Descendant Selector: Selects elements that are descendants of another element. Example:
$('div p')
selects all<p>
elements that are descendants of<div>
elements.Child Selector: Selects elements that are direct children of another element. Example:
$('ul > li')
selects all<li>
elements that are direct children of<ul>
elements.Parent Selector: Selects elements that are parents of another element. Example:
$('p').parent()
selects the parent elements of all<p>
elements.Sibling Selector: Selects elements that share the same parent and come after another element. Example:
$('h2 + p')
selects all<p>
elements that come immediately after<h2>
elements.Filter Selector: Selects elements that match a specific condition. Example:
$('li').filter('.highlighted')
selects all<li>
elements with the class “highlighted”.:first Selector: Selects the first element that matches the selector. Example:
$('p:first')
selects the first<p>
element.
These are just a few examples of jQuery selectors. There are many more available, and you can also combine multiple selectors to create complex queries. jQuery documentation provides detailed information about all the available selectors and their usage.
- 9 hours of video
- 1 article or resource
- Certificate of completion
- Last updated 10/2021